diff --git a/.github/workflows/BuildAndPack.yml b/.github/workflows/BuildAndPack.yml index cdd141ece..d01eb547b 100644 --- a/.github/workflows/BuildAndPack.yml +++ b/.github/workflows/BuildAndPack.yml @@ -58,63 +58,63 @@ jobs: with: name: artifacts path: artifacts - windows-latest: - name: windows-latest - runs-on: windows-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: | - 7.0.x - 6.0.x - 5.0.x - 3.1.x - - name: Cache .nuke/temp, ~/.nuget/packages - uses: actions/cache@v2 - with: - path: | - .nuke/temp - ~/.nuget/packages - !~/.nuget/packages/stronglytypeid - key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} - - name: Run './build.cmd Clean Test TestPackages PushToNuGet' - run: ./build.cmd Clean Test TestPackages PushToNuGet - env: - GithubToken: ${{ secrets.GITHUB_TOKEN }} - NuGetToken: ${{ secrets.NUGET_TOKEN }} - MSBuildEnableWorkloadResolver: false - - uses: actions/upload-artifact@v1 - with: - name: artifacts - path: artifacts - macOS-latest: - name: macOS-latest - runs-on: macOS-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-dotnet@v3 - with: - dotnet-version: | - 7.0.x - 6.0.x - 5.0.x - 3.1.x - - name: Cache .nuke/temp, ~/.nuget/packages - uses: actions/cache@v2 - with: - path: | - .nuke/temp - ~/.nuget/packages - !~/.nuget/packages/stronglytypeid - key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} - - name: Run './build.cmd Clean Test TestPackages PushToNuGet' - run: ./build.cmd Clean Test TestPackages PushToNuGet - env: - GithubToken: ${{ secrets.GITHUB_TOKEN }} - NuGetToken: ${{ secrets.NUGET_TOKEN }} - MSBuildEnableWorkloadResolver: false - - uses: actions/upload-artifact@v1 - with: - name: artifacts - path: artifacts +# windows-latest: +# name: windows-latest +# runs-on: windows-latest +# steps: +# - uses: actions/checkout@v3 +# - uses: actions/setup-dotnet@v3 +# with: +# dotnet-version: | +# 7.0.x +# 6.0.x +# 5.0.x +# 3.1.x +# - name: Cache .nuke/temp, ~/.nuget/packages +# uses: actions/cache@v2 +# with: +# path: | +# .nuke/temp +# ~/.nuget/packages +# !~/.nuget/packages/stronglytypeid +# key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} +# - name: Run './build.cmd Clean Test TestPackages PushToNuGet' +# run: ./build.cmd Clean Test TestPackages PushToNuGet +# env: +# GithubToken: ${{ secrets.GITHUB_TOKEN }} +# NuGetToken: ${{ secrets.NUGET_TOKEN }} +# MSBuildEnableWorkloadResolver: false +# - uses: actions/upload-artifact@v1 +# with: +# name: artifacts +# path: artifacts +# macOS-latest: +# name: macOS-latest +# runs-on: macOS-latest +# steps: +# - uses: actions/checkout@v3 +# - uses: actions/setup-dotnet@v3 +# with: +# dotnet-version: | +# 7.0.x +# 6.0.x +# 5.0.x +# 3.1.x +# - name: Cache .nuke/temp, ~/.nuget/packages +# uses: actions/cache@v2 +# with: +# path: | +# .nuke/temp +# ~/.nuget/packages +# !~/.nuget/packages/stronglytypeid +# key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj') }} +# - name: Run './build.cmd Clean Test TestPackages PushToNuGet' +# run: ./build.cmd Clean Test TestPackages PushToNuGet +# env: +# GithubToken: ${{ secrets.GITHUB_TOKEN }} +# NuGetToken: ${{ secrets.NUGET_TOKEN }} +# MSBuildEnableWorkloadResolver: false +# - uses: actions/upload-artifact@v1 +# with: +# name: artifacts +# path: artifacts diff --git a/Directory.Build.props b/Directory.Build.props index 54c03fcd5..a879488ad 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -10,9 +10,9 @@ false MIT logo.png - https://github.com/andrewlock/StronglyTypedId + https://github.com/tennisi/StronglyTypedId stronglytypedid attribute generator generation codegen codegenerator codegeneration netescapades - https://github.com/andrewlock/StronglyTypedId + https://github.com/tennisi/StronglyTypedId git false @@ -42,4 +42,4 @@ - \ No newline at end of file + diff --git a/README.md b/README.md index 094fdf587..dff946975 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ To use the the [StronglyTypedId NuGet package](https://www.nuget.org/packages/St * [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/) (optional, only required if [generating a System.Text `JsonConverter`](https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-2/#creating-a-custom-jsonconverter)). Note that in .NET Core apps, you will likely already reference this project via transitive dependencies. * [Dapper](https://www.nuget.org/packages/Dapper/) (optional, only required if [generating a type mapper](https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-3/#interfacing-with-external-system-using-strongly-typed-ids)) * [EF Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) (optional, only required if [generating an EF Core ValueConverter](https://andrewlock.net/strongly-typed-ids-in-ef-core-using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-4/)) +* [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) (optional, only required if [generating an Swagger Schema Filter](#openapiswagger-specification) + To install the packages, add the references to your _csproj_ file, for example by running @@ -160,6 +162,18 @@ public partial struct OrderId { } public partial struct UserId { } ``` +## OpenApi/Swagger Specification + +If you wish to use an ID in your Swagger models and want to have schema and model sample reflecting the ID backingfield type you will need: +- Install [Swagger Annotations](https://www.nuget.org/packages/Swashbuckle.AspNetCore.Annotations) `>=5.0.0` +- Enable annotation in swagger gen with `services.AddSwaggerGen(c => c.EnableAnnotations());` +- Use the converter flag `StronglyTypedIdConverter.SwaggerSchemaFilter` on the ID decorator. eg: + ```csharp + [StronglyTypedId( + backingType: StronglyTypedIdBackingType.Int, + converters: StronglyTypedIdConverter.SwaggerSchemaFilter | StronglyTypedIdConverter.SystemTextJson)] + public partial struct UserId { } + ``` ## Embedding the attributes in your project @@ -253,4 +267,4 @@ The `struct`s you decorate with the `StronglyTypedId` attribute must be marked ` `StronglyTypedId` wouldn't work if not for [AArnott's CodeGeneration.Roslyn](https://github.com/AArnott/CodeGeneration.Roslyn) library. -The build process and general design of the library was modelled on the [RecordGenerator](https://github.com/amis92/RecordGenerator/blob/master/README.md) project, which is similar to this project, but can be used to generate immutable Record types. \ No newline at end of file +The build process and general design of the library was modelled on the [RecordGenerator](https://github.com/amis92/RecordGenerator/blob/master/README.md) project, which is similar to this project, but can be used to generate immutable Record types. diff --git a/StronglyTypedId.sln b/StronglyTypedId.sln index cd44309c6..39ac8adc9 100644 --- a/StronglyTypedId.sln +++ b/StronglyTypedId.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28803.352 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33122.133 MinimumVisualStudioVersion = 15.0.26124.0 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EE1258BD-3422-4F55-B9CF-B4D6C95DAD68}" EndProject @@ -16,21 +16,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md releasenotes.props = releasenotes.props version.props = version.props + .github\workflows\BuildAndPack.yml = .github\workflows\BuildAndPack.yml + CHANGELOG.md = CHANGELOG.md + build.sh = build.sh + NuGet.integration-tests.config = NuGet.integration-tests.config + global.json = global.json EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{E13FB452-2D47-4719-8BAA-7B695D79AF3A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -42,8 +47,6 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -80,6 +83,16 @@ Global {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x64.Build.0 = Release|Any CPU {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.ActiveCfg = Release|Any CPU {09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.Build.0 = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.Build.0 = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.ActiveCfg = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.Build.0 = Debug|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.Build.0 = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.ActiveCfg = Release|Any CPU + {E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.Build.0 = Release|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.Build.0 = Debug|Any CPU {F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|x64.ActiveCfg = Debug|Any CPU diff --git a/build/Build.cs b/build/Build.cs index 39bb2c919..43c63a6c0 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -146,7 +146,7 @@ class Build : NukeBuild Target PushToNuGet => _ => _ .DependsOn(Compile) - .OnlyWhenStatic(() => IsTag && IsServerBuild && IsWin) + .OnlyWhenStatic(() => true || ( IsTag && IsServerBuild && IsWin )) .Requires(() => NuGetToken) .After(Pack) .Executes(() => diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs index 599a51409..4dad18ae1 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs @@ -46,5 +46,11 @@ public enum StronglyTypedIdConverter /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } \ No newline at end of file diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs index d0900fc4f..b323f597a 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs @@ -8,7 +8,7 @@ namespace StronglyTypedIds [Flags] public enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -33,5 +33,11 @@ public enum StronglyTypedIdImplementations /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj b/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj index 5fd0485d3..415ff92f1 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj @@ -2,8 +2,8 @@ netstandard2.0 - enable StronglyTypedIds + enable false diff --git a/src/StronglyTypedIds/Constants.cs b/src/StronglyTypedIds/Constants.cs index 5cb590a6b..62911130a 100644 --- a/src/StronglyTypedIds/Constants.cs +++ b/src/StronglyTypedIds/Constants.cs @@ -1,7 +1,9 @@ +using System; + namespace StronglyTypedIds { internal static class Constants { public const string Usage = nameof(Usage); } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index e1f229779..bcd8f008a 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,6 +25,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"), false ); @@ -37,6 +39,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"), false ); @@ -49,6 +53,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"), false ); @@ -61,6 +67,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"), false ); @@ -73,6 +81,8 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"), true ); @@ -85,12 +95,15 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"), LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Parsable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.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 SwaggerSchemaFilterAttributeSource = " [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(TESTIDSchemaFilter))]"; internal static string LoadEmbeddedResource(string resourceName) { @@ -108,6 +121,7 @@ internal static string LoadEmbeddedResource(string resourceName) public readonly struct ResourceCollection { + public string SwaggerSchemaFilter { get; } public string Header { get; } public bool NullableEnable { get; } public string BaseId { get; } @@ -117,6 +131,7 @@ public readonly struct ResourceCollection public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } public string Comparable { get; } + public string Parsable { get; } public ResourceCollection( string header, @@ -127,8 +142,11 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, + string parsable, + string swaggerSchemaFilter, bool nullableEnable) { + SwaggerSchemaFilter = swaggerSchemaFilter; BaseId = baseId; Newtonsoft = newtonsoft; SystemTextJson = systemTextJson; @@ -136,6 +154,7 @@ public ResourceCollection( EfCoreValueConverter = efCoreValueConverter; DapperTypeHandler = dapperTypeHandler; Comparable = comparable; + Parsable = parsable; NullableEnable = nullableEnable; Header = header; } @@ -153,4 +172,4 @@ internal static string LoadTemplateForEmitting(string resourceName) #endif"; } } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index e33515310..265225909 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -64,6 +64,7 @@ static string CreateId( var hasNamespace = !string.IsNullOrEmpty(idNamespace); + var useSchemaFilter = converters.IsSet(StronglyTypedIdConverter.SwaggerSchemaFilter); var useTypeConverter = converters.IsSet(StronglyTypedIdConverter.TypeConverter); var useNewtonsoftJson = converters.IsSet(StronglyTypedIdConverter.NewtonsoftJson); var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson); @@ -72,6 +73,7 @@ static string CreateId( var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable); var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable); + var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable); var parentsCount = 0; @@ -122,8 +124,14 @@ static string CreateId( sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource); } + if (useSchemaFilter) + { + sb.AppendLine(EmbeddedSources.SwaggerSchemaFilterAttributeSource); + } + + sb.Append(resources.BaseId); - ReplaceInterfaces(sb, useIEquatable, useIComparable); + ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable); // IEquatable is already implemented whether or not the interface is implemented @@ -132,6 +140,11 @@ static string CreateId( sb.AppendLine(resources.Comparable); } + if (useIParsable) + { + sb.AppendLine(resources.Parsable); + } + if (useEfCoreValueConverter) { sb.AppendLine(resources.EfCoreValueConverter); @@ -157,6 +170,11 @@ static string CreateId( sb.AppendLine(resources.SystemTextJson); } + if (useSchemaFilter) + { + sb.AppendLine(resources.SwaggerSchemaFilter); + } + sb.Replace("TESTID", idName); sb.AppendLine(@" }"); @@ -173,7 +191,7 @@ static string CreateId( return sb.ToString(); } - private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable) + private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable) { var interfaces = new List(); @@ -187,6 +205,11 @@ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool interfaces.Add("System.IEquatable"); } + if (useIParsable) + { + interfaces.Add("System.IParsable"); + } + if (interfaces.Count > 0) { sb.Replace("INTERFACES", string.Join(", ", interfaces)); diff --git a/src/StronglyTypedIds/StronglyTypedIds.csproj b/src/StronglyTypedIds/StronglyTypedIds.csproj index 64f63c39f..b3cd8e94d 100644 --- a/src/StronglyTypedIds/StronglyTypedIds.csproj +++ b/src/StronglyTypedIds/StronglyTypedIds.csproj @@ -4,7 +4,7 @@ netstandard2.0 false enable - StronglyTypedId + Tennisi.StronglyTypedId A source generator for creating strongly-typed IDs by decorating with a [StronglyTypedId] attribute @@ -26,4 +26,4 @@ - \ No newline at end of file + diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs index 200147a15..5f980f97a 100644 --- a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs @@ -7,6 +7,10 @@ public TESTID(System.Guid value) Value = value; } + public TESTID() + { + } + public static TESTID New() => new TESTID(System.Guid.NewGuid()); public static readonly TESTID Empty = new TESTID(System.Guid.Empty); diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs index 7d94a235e..7b48c73b1 100644 --- a/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs @@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override TESTID Parse(object value) @@ -15,4 +16,12 @@ public override TESTID Parse(object value) _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs new file mode 100644 index 000000000..85f7095a7 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs @@ -0,0 +1,15 @@ + +#nullable enable + public static TESTID Parse(string s, System.IFormatProvider? provider) + { + return new TESTID(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..de06e10c0 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_SwaggerSchemaFilter.cs @@ -0,0 +1,13 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Int/Int_Base.cs b/src/StronglyTypedIds/Templates/Int/Int_Base.cs index 08ce1fca6..04613630c 100644 --- a/src/StronglyTypedIds/Templates/Int/Int_Base.cs +++ b/src/StronglyTypedIds/Templates/Int/Int_Base.cs @@ -7,6 +7,10 @@ public TESTID(int value) Value = value; } + public TESTID() + { + } + public static readonly TESTID Empty = new TESTID(0); public bool Equals(TESTID other) => this.Value.Equals(other.Value); diff --git a/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs index 8e3dc42c9..2da54911b 100644 --- a/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs @@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; } public override TESTID Parse(object value) @@ -16,4 +17,13 @@ public override TESTID Parse(object value) _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } diff --git a/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs b/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs new file mode 100644 index 000000000..f5893b0ca --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_Parsable.cs @@ -0,0 +1,15 @@ + +#nullable enable + public static TESTID Parse(string s, System.IFormatProvider? provider) + { + return new TESTID(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..34b0d8919 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_SwaggerSchemaFilter.cs @@ -0,0 +1,13 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Long/Long_Base.cs b/src/StronglyTypedIds/Templates/Long/Long_Base.cs index 38d9fc5a6..77f7dd184 100644 --- a/src/StronglyTypedIds/Templates/Long/Long_Base.cs +++ b/src/StronglyTypedIds/Templates/Long/Long_Base.cs @@ -7,6 +7,10 @@ public TESTID(long value) Value = value; } + public TESTID() + { + } + public static readonly TESTID Empty = new TESTID(0); public bool Equals(TESTID other) => this.Value.Equals(other.Value); diff --git a/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs index 42dac85c9..e8f11b11c 100644 --- a/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs @@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; } public override TESTID Parse(object value) @@ -12,9 +13,18 @@ public override TESTID Parse(object value) { long longValue => new TESTID(longValue), int intValue => new TESTID(intValue), + decimal decimalValue => new TESTID((long)decimalValue), short shortValue => new TESTID(shortValue), string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new TESTID(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs b/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs new file mode 100644 index 000000000..3a63073f9 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_Parsable.cs @@ -0,0 +1,15 @@ + +#nullable enable + public static TESTID Parse(string s, System.IFormatProvider? provider) + { + return new TESTID(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..0d3508292 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_SwaggerSchemaFilter.cs @@ -0,0 +1,13 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs index 854cd477a..4463479c2 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs @@ -7,6 +7,10 @@ public TESTID(MassTransit.NewId value) Value = value; } + public TESTID() + { + } + public static TESTID New() => new TESTID(MassTransit.NewId.Next()); public static readonly TESTID Empty = new TESTID(MassTransit.NewId.Empty); diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs index 8d4af7b02..04e3693b5 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs @@ -15,4 +15,12 @@ public override TESTID Parse(object value) _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs new file mode 100644 index 000000000..2faf43e0d --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_Parsable.cs @@ -0,0 +1,13 @@ + + public static TESTID Parse(string s, IFormatProvider? provider) + { + return new TESTID(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..de06e10c0 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_SwaggerSchemaFilter.cs @@ -0,0 +1,13 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs index 94a6eaffb..3e7a28c12 100644 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs @@ -7,6 +7,10 @@ public TESTID(string? value) Value = value; } + public TESTID() + { + } + public static readonly TESTID Empty = new TESTID(string.Empty); public bool Equals(TESTID other) diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs index 1524a13b1..91720bcc8 100644 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs @@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override TESTID Parse(object value) @@ -16,4 +17,12 @@ public override TESTID Parse(object value) _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs new file mode 100644 index 000000000..1cd9b15e1 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Parsable.cs @@ -0,0 +1,11 @@ + + public static TESTID Parse(string s, IFormatProvider? provider) + { + return new TESTID(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result) + { + result = new TESTID(s); + return true; + } diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..daea45b8d --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_SwaggerSchemaFilter.cs @@ -0,0 +1,14 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_Base.cs b/src/StronglyTypedIds/Templates/String/String_Base.cs index 023df780b..8855711c6 100644 --- a/src/StronglyTypedIds/Templates/String/String_Base.cs +++ b/src/StronglyTypedIds/Templates/String/String_Base.cs @@ -7,6 +7,10 @@ public TESTID(string value) Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public TESTID() + { + } + public static readonly TESTID Empty = new TESTID(string.Empty); public bool Equals(TESTID other) diff --git a/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs index 93d6ff7bf..9b0f11bc6 100644 --- a/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs @@ -4,6 +4,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override TESTID Parse(object value) @@ -14,4 +15,12 @@ public override TESTID Parse(object value) _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } - } \ No newline at end of file + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/src/StronglyTypedIds/Templates/String/String_Parsable.cs b/src/StronglyTypedIds/Templates/String/String_Parsable.cs new file mode 100644 index 000000000..769bd1bb2 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_Parsable.cs @@ -0,0 +1,11 @@ + + public static TESTID Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result) + { + result = new TESTID(s); + return true; + } diff --git a/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs b/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs new file mode 100644 index 000000000..1073bbf27 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_SwaggerSchemaFilter.cs @@ -0,0 +1,14 @@ + + class TESTIDSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } \ No newline at end of file diff --git a/test/IntegrationLibraries.props b/test/IntegrationLibraries.props index 8ba7bf0b0..f4fd1dc04 100644 --- a/test/IntegrationLibraries.props +++ b/test/IntegrationLibraries.props @@ -3,12 +3,14 @@ + + diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs index 2905f70e6..2ce11869c 100644 --- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs @@ -325,6 +325,28 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerGuidId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("string", schema.Type); + Assert.Equal("uuid", schema.Format); + } +#endif + public class TestDbContext : DbContext { public DbSet Entities { get; set; } @@ -344,6 +366,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .ValueGeneratedNever(); }); } + } public class TestEntity diff --git a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs index e2c48be64..90000025c 100644 --- a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs @@ -318,6 +318,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerIntId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("integer", schema.Type); + Assert.Equal("int32", schema.Format); + } +#endif public class TestDbContext : DbContext { public DbSet Entities { get; set; } diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs index b2f70dbcb..404ca9257 100644 --- a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs @@ -319,6 +319,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerLongId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("integer", schema.Type); + Assert.Equal("int64", schema.Format); + } +#endif public class TestDbContext : DbContext { public DbSet Entities { get; set; } diff --git a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs index e6dce6d74..ef1e75f8d 100644 --- a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs @@ -327,6 +327,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerNewIdId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("string", schema.Type); + Assert.Equal("uuid", schema.Format); + } +#endif public class TestDbContext : DbContext { public DbSet Entities { get; set; } diff --git a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs index c6835af82..671cfceb6 100644 --- a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs @@ -395,6 +395,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerNullableStringId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("string", schema.Type); + Assert.Equal("", schema.Format); + } +#endif public class TestDbContext : DbContext { public DbSet Entities { get; set; } diff --git a/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs b/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs new file mode 100644 index 000000000..403fccc2f --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/ParsableTests.cs @@ -0,0 +1,6 @@ +namespace StronglyTypedIds.IntegrationTests; + +public class ParsableTests +{ + +} \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs index 0cc76b44d..f4057089b 100644 --- a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs @@ -329,6 +329,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) } #endif +#if NET5_0_OR_GREATER + [Fact] + public void CanShowImplementationTypeExample_WithSwaggerSchemaFilter() + { + var schemaGenerator = new Swashbuckle.AspNetCore.SwaggerGen.SchemaGenerator( + new Swashbuckle.AspNetCore.SwaggerGen.SchemaGeneratorOptions(), + new Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver( + new System.Text.Json.JsonSerializerOptions())); + var provider = Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider( + new Microsoft.Extensions.DependencyInjection.ServiceCollection()); + var schemaFilter = new Swashbuckle.AspNetCore.Annotations.AnnotationsSchemaFilter(provider); + var schemaRepository = new Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository(); + + var idType = typeof(SwaggerStringId); + var schema = schemaGenerator.GenerateSchema(idType, schemaRepository); + schemaFilter.Apply(schema, new Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext(idType, schemaGenerator, schemaRepository)); + + Assert.Equal("string", schema.Type); + Assert.Equal("", schema.Format); + } +#endif public class TestDbContext : DbContext { public DbSet Entities { get; set; } diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj index 2800167d3..178bffc70 100644 --- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj @@ -4,6 +4,7 @@ netcoreapp3.1;net6.0;net7.0 net48;$(TargetFrameworks) false + disable true @@ -11,6 +12,11 @@ + + + + + diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs index 9ad651d72..dcafd4207 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs @@ -29,6 +29,11 @@ public partial struct EfCoreGuidId { } [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler)] public partial struct DapperGuidId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter)] + public partial struct SwaggerGuidId { } +#endif + [StronglyTypedId(implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothGuidId { } diff --git a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs index 15316b98a..809335237 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs @@ -26,6 +26,11 @@ public partial struct EfCoreIntId { } [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Int)] public partial struct DapperIntId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.Int)] + public partial struct SwaggerIntId { } +#endif + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Int, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothIntId { } diff --git a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs index 5a1081bb8..b3fcf5667 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs @@ -26,6 +26,11 @@ public partial struct EfCoreLongId { } [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.Long)] public partial struct DapperLongId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.Long)] + public partial struct SwaggerLongId { } +#endif + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothLongId { } diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs index 0be4ce4c9..6e6f5b75e 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs @@ -29,6 +29,11 @@ public partial struct EfCoreNewIdId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.DapperTypeHandler)] public partial struct DapperNewIdId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.SwaggerSchemaFilter)] + public partial struct SwaggerNewIdId { } +#endif + [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothNewIdId { } diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs index d51e901a1..b98962246 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs @@ -26,6 +26,11 @@ public partial struct EfCoreNullableStringId { } [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.NullableString)] public partial struct DapperNullableStringId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.NullableString)] + public partial struct SwaggerNullableStringId { } +#endif + [StronglyTypedId(backingType: StronglyTypedIdBackingType.NullableString, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothNullableStringId { } diff --git a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs index 5e7cdfa57..bf8a81afa 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs @@ -26,6 +26,11 @@ public partial struct EfCoreStringId { } [StronglyTypedId(converters: StronglyTypedIdConverter.DapperTypeHandler, backingType: StronglyTypedIdBackingType.String)] public partial struct DapperStringId { } +#if NET5_0_OR_GREATER + [StronglyTypedId(converters: StronglyTypedIdConverter.SwaggerSchemaFilter, backingType: StronglyTypedIdBackingType.String)] + public partial struct SwaggerStringId { } +#endif + [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] public partial struct BothStringId { } 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 16fb810fc..17a628451 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -17,7 +17,7 @@ - + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index 38b3c43f8..4747ca652 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -16,7 +16,7 @@ - + diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt new file mode 100644 index 000000000..f9fffe68a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..f9fffe68a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..f9fffe68a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt new file mode 100644 index 000000000..f9fffe68a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#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..f9fffe68a 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,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt new file mode 100644 index 000000000..3eb5342b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt new file mode 100644 index 000000000..3eb5342b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt new file mode 100644 index 000000000..3eb5342b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_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 + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt new file mode 100644 index 000000000..3eb5342b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt index 76835ffec..3eb5342b4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.verified.txt @@ -21,7 +21,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -46,6 +46,13 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt new file mode 100644 index 000000000..ed774067b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt new file mode 100644 index 000000000..ed774067b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt new file mode 100644 index 000000000..ed774067b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt new file mode 100644 index 000000000..ed774067b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt index d7a3ed3e7..ed774067b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt @@ -12,7 +12,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public System.Guid Value { get; } @@ -21,6 +22,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); @@ -38,6 +43,22 @@ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -54,6 +75,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) @@ -65,8 +87,17 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +171,17 @@ writer.WriteStringValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt new file mode 100644 index 000000000..ef397bdd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt new file mode 100644 index 000000000..ef397bdd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt new file mode 100644 index 000000000..ef397bdd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt new file mode 100644 index 000000000..ef397bdd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt index e7affbacb..ef397bdd8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt @@ -12,7 +12,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public int Value { get; } @@ -21,6 +22,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); @@ -37,6 +42,22 @@ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -53,6 +74,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; } public override MyTestId Parse(object value) @@ -65,8 +87,18 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +172,17 @@ writer.WriteNumberValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt new file mode 100644 index 000000000..82026ab16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt @@ -0,0 +1,191 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt new file mode 100644 index 000000000..82026ab16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt @@ -0,0 +1,191 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt new file mode 100644 index 000000000..82026ab16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt @@ -0,0 +1,191 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt new file mode 100644 index 000000000..82026ab16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt @@ -0,0 +1,191 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt index 0a464e952..82026ab16 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt @@ -12,7 +12,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public long Value { get; } @@ -21,6 +22,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); @@ -37,6 +42,22 @@ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -53,6 +74,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; } public override MyTestId Parse(object value) @@ -61,13 +83,23 @@ { long longValue => new MyTestId(longValue), int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), short shortValue => new MyTestId(shortValue), string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -143,4 +175,17 @@ writer.WriteNumberValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt new file mode 100644 index 000000000..875ca2bdf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt @@ -0,0 +1,192 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt new file mode 100644 index 000000000..875ca2bdf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt @@ -0,0 +1,192 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt new file mode 100644 index 000000000..875ca2bdf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt @@ -0,0 +1,192 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt new file mode 100644 index 000000000..875ca2bdf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt @@ -0,0 +1,192 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt index c2c044246..875ca2bdf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt @@ -12,7 +12,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public MassTransit.NewId Value { get; } @@ -21,6 +22,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); @@ -38,6 +43,20 @@ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -65,8 +84,17 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -148,4 +176,17 @@ writer.WriteStringValue(value.Value.ToGuid()); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt new file mode 100644 index 000000000..898bacee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt @@ -0,0 +1,216 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt new file mode 100644 index 000000000..898bacee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt @@ -0,0 +1,216 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt new file mode 100644 index 000000000..898bacee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt @@ -0,0 +1,216 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt new file mode 100644 index 000000000..898bacee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt @@ -0,0 +1,216 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt index 975c29ade..898bacee6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt @@ -13,7 +13,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public string? Value { get; } @@ -22,6 +23,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) @@ -55,6 +60,18 @@ }; } + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -71,6 +88,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override MyTestId Parse(object value) @@ -83,8 +101,17 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) @@ -172,4 +199,18 @@ } } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt new file mode 100644 index 000000000..672589af9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt @@ -0,0 +1,202 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt new file mode 100644 index 000000000..672589af9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt @@ -0,0 +1,202 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt new file mode 100644 index 000000000..672589af9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt @@ -0,0 +1,202 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt new file mode 100644 index 000000000..672589af9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt @@ -0,0 +1,202 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt index 3e93ee6b2..672589af9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt @@ -12,7 +12,8 @@ [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 + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable { public string Value { get; } @@ -21,6 +22,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) @@ -55,6 +60,18 @@ }; } + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -71,6 +88,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override MyTestId Parse(object value) @@ -81,8 +99,17 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -158,4 +185,18 @@ writer.WriteStringValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..95ab1cbb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..95ab1cbb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..95ab1cbb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..95ab1cbb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt index bb6b344ab..95ab1cbb9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); @@ -39,6 +43,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) @@ -50,5 +55,14 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..5c8b31538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..5c8b31538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..5c8b31538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..5c8b31538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.verified.txt index 508208c37..5c8b31538 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..6dc2c9809 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..6dc2c9809 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..6dc2c9809 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..6dc2c9809 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt index db59f66c1..6dc2c9809 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..c05105d8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Core3_1.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..c05105d8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..c05105d8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Guid_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..c05105d8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Guid_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.verified.txt index bef90429e..c05105d8b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..298a9ab68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..298a9ab68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet6_0.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..298a9ab68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet7_0.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 + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..298a9ab68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.verified.txt index a13e9bec6..298a9ab68 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..4eace68dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Core3_1.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..4eace68dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..4eace68dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..4eace68dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..4eace68dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..7bd7298ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..7bd7298ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet6_0.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..7bd7298ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet7_0.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 + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..7bd7298ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Guid_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.verified.txt index fad5ea0b5..7bd7298ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..7433dc1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Core3_1.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..7433dc1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..7433dc1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..7433dc1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..7433dc1ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..657eee50a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..657eee50a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet6_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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..657eee50a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet7_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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..657eee50a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.verified.txt index d6d801a99..657eee50a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..ec9fe7152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..ec9fe7152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet6_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..ec9fe7152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet7_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..ec9fe7152 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt index 295e92baf..ec9fe7152 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..1a359ab35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Core3_1.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..1a359ab35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..1a359ab35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..1a359ab35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt index f8d24bbf5..1a359ab35 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); @@ -38,6 +42,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; } public override MyTestId Parse(object value) @@ -50,5 +55,15 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..4538584cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Core3_1.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..4538584cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..4538584cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..4538584cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt index d3e50467b..4538584cf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..a9d0a0884 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Core3_1.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..a9d0a0884 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..a9d0a0884 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..a9d0a0884 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt index b4d9738e7..a9d0a0884 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..f7b93a1a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.Core3_1.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Int_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..f7b93a1a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.DotNet6_0.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Int_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..f7b93a1a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.DotNet7_0.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 + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Int_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..f7b93a1a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Int_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt index d76d3afd2..f7b93a1a4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..71ecb8822 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.Core3_1.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..71ecb8822 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.DotNet6_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 + + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..71ecb8822 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.DotNet7_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 + + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..71ecb8822 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt index 5d23b64a1..71ecb8822 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..d64a059e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Core3_1.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.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..d64a059e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet6_0.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.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..d64a059e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.DotNet7_0.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.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..d64a059e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..d64a059e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IParsable.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.IParsable + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..83b72d9c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.Core3_1.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..83b72d9c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..83b72d9c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..83b72d9c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Int_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.verified.txt index 45272d34a..83b72d9c3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..6d5900132 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Core3_1.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..6d5900132 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet6_0.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..6d5900132 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.DotNet7_0.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..6d5900132 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.Net4_8.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..6d5900132 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SwaggerSchemaFilter_i=None.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..b31b2b527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Core3_1.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..b31b2b527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet6_0.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..b31b2b527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.DotNet7_0.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..b31b2b527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt index 7c016c3be..b31b2b527 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..fe1551202 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Core3_1.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..fe1551202 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet6_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..fe1551202 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.DotNet7_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..fe1551202 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt index 39f1e9331..fe1551202 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..6d9709fa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..6d9709fa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..6d9709fa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..6d9709fa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt index 35fd482b5..6d9709fa1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); @@ -38,6 +42,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; } public override MyTestId Parse(object value) @@ -46,10 +51,20 @@ { long longValue => new MyTestId(longValue), int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), short shortValue => new MyTestId(shortValue), string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..9f14b9ac7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Core3_1.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..9f14b9ac7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..9f14b9ac7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..9f14b9ac7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt index cb5754863..9f14b9ac7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..e6ebafd65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Core3_1.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..e6ebafd65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..e6ebafd65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..e6ebafd65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt index 7052a3b27..e6ebafd65 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..16e1e540f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.Core3_1.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Long_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..16e1e540f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.DotNet6_0.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Long_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..16e1e540f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.DotNet7_0.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 + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Long_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..16e1e540f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=Long_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.verified.txt index c38fa63f6..16e1e540f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..10c29a473 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.Core3_1.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..10c29a473 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.DotNet6_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 + + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..10c29a473 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.DotNet7_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 + + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..10c29a473 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt index 13b9b1ac7..10c29a473 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..023858bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Core3_1.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.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..023858bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet6_0.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.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..023858bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.DotNet7_0.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.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..023858bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..023858bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IParsable.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.IParsable + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..bf54ff265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.Core3_1.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..bf54ff265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..bf54ff265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..bf54ff265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=Long_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt index af1658c35..bf54ff265 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..61475fdf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Core3_1.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..61475fdf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet6_0.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..61475fdf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.DotNet7_0.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..61475fdf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.Net4_8.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..61475fdf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SwaggerSchemaFilter_i=None.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..c7e7acc44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Core3_1.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..c7e7acc44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet6_0.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..c7e7acc44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.DotNet7_0.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..c7e7acc44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt index 17a45333f..c7e7acc44 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..04b427147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Core3_1.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..04b427147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet6_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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..04b427147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.DotNet7_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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..04b427147 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt index e22d0478c..04b427147 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(0); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..a6f4c14a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Core3_1.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..a6f4c14a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..a6f4c14a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..a6f4c14a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,67 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, 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"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt index 23efe4b09..a6f4c14a8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); @@ -50,5 +54,14 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..516ad6621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..516ad6621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..516ad6621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..516ad6621 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt index 9114f2d54..516ad6621 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..4e3b7d682 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Core3_1.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..4e3b7d682 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..4e3b7d682 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..4e3b7d682 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt index 181d85e44..4e3b7d682 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..6f9e66a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.Core3_1.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=MassTransitNewId_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..6f9e66a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.DotNet6_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=MassTransitNewId_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..6f9e66a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.DotNet7_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=MassTransitNewId_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..6f9e66a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, 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_type=MassTransitNewId_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt index 3a19fc65e..6f9e66a60 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..7f737b58a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.Core3_1.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..7f737b58a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.DotNet6_0.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..7f737b58a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.DotNet7_0.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..7f737b58a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt index b4c2d5867..7f737b58a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..d899e03df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Core3_1.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.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..d899e03df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet6_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 : System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..d899e03df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.DotNet7_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 : System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..d899e03df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..d899e03df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IParsable.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.IParsable + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..b269a6f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..b269a6f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.DotNet6_0.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..b269a6f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.DotNet7_0.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 + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..b269a6f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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_type=MassTransitNewId_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt index 7e4892f41..b269a6f5c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..dd6ee0e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Core3_1.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..dd6ee0e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet6_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..dd6ee0e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.DotNet7_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..dd6ee0e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..dd6ee0e3d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SwaggerSchemaFilter_i=None.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..9b6a12ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Core3_1.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..9b6a12ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet6_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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..9b6a12ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.DotNet7_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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..9b6a12ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,54 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt index 3de0600ab..9b6a12ba4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..ca57c53a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Core3_1.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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..ca57c53a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet6_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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..ca57c53a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.DotNet7_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 MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..ca57c53a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt index 757eb4ebb..ca57c53a6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..71d558a1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Core3_1.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 + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..71d558a1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..71d558a1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..71d558a1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt index c96f1e168..71d558a1a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) @@ -47,6 +51,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override MyTestId Parse(object value) @@ -59,5 +64,14 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..99739c312 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Core3_1.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..99739c312 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..99739c312 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..99739c312 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt index e1a174104..99739c312 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..e7039bc1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Core3_1.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..e7039bc1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet6_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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..e7039bc1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.DotNet7_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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..e7039bc1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt index 9660c1fc0..e7039bc1c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt @@ -20,6 +20,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..bac7f46ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Core3_1.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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..bac7f46ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..bac7f46ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..bac7f46ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IComparable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt index d779d1af4..bac7f46ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..4f7e9865c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.Core3_1.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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..4f7e9865c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..4f7e9865c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..4f7e9865c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IEquatable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.verified.txt index bb2e2c7d2..4f7e9865c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..60a9d1907 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Core3_1.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..60a9d1907 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId : System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..60a9d1907 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId : System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..60a9d1907 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..60a9d1907 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IParsable.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId : System.IParsable + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..64c0520a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.Core3_1.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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..64c0520a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.DotNet6_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..64c0520a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.DotNet7_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 + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..64c0520a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + 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_type=NullableString_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.verified.txt index fe1e43665..64c0520a3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..e0e53762f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Core3_1.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 + +#nullable enable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..e0e53762f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet6_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 + +#nullable enable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..e0e53762f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.DotNet7_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 + +#nullable enable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..e0e53762f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..e0e53762f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SwaggerSchemaFilter_i=None.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 + +#nullable enable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..64c8a2195 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Core3_1.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..64c8a2195 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet6_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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..64c8a2195 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.DotNet7_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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..64c8a2195 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.verified.txt index c049fb7f5..64c8a2195 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.verified.txt @@ -20,6 +20,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..52070ac2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Core3_1.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..52070ac2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet6_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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..52070ac2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.DotNet7_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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..52070ac2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt index 50b3c0585..52070ac2a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt @@ -20,6 +20,10 @@ Value = value; } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..67b1ce8c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..67b1ce8c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..67b1ce8c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..67b1ce8c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt index 749dd1dbd..67b1ce8c8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) @@ -47,6 +51,7 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; } public override MyTestId Parse(object value) @@ -57,5 +62,14 @@ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..da60d2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Core3_1.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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..da60d2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..da60d2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..da60d2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt index 05700f78d..da60d2fe0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..2fe8b1fc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Core3_1.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 + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..2fe8b1fc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet6_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..2fe8b1fc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.DotNet7_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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..2fe8b1fc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt index d7596bf2b..2fe8b1fc6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Core3_1.verified.txt new file mode 100644 index 000000000..7c47a4192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Core3_1.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet6_0.verified.txt new file mode 100644 index 000000000..7c47a4192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet6_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 + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet7_0.verified.txt new file mode 100644 index 000000000..7c47a4192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.DotNet7_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 + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..7c47a4192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt index 4fae5139a..7c47a4192 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt @@ -18,6 +18,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.Core3_1.verified.txt new file mode 100644 index 000000000..e167942a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.Core3_1.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=IEquatable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.DotNet6_0.verified.txt new file mode 100644 index 000000000..e167942a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.DotNet6_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 + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=IEquatable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.DotNet7_0.verified.txt new file mode 100644 index 000000000..e167942a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.DotNet7_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 + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..e167942a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.verified.txt index 050bcc20b..e167942a4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.verified.txt @@ -18,6 +18,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..ba5c87baf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Core3_1.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 + + readonly partial struct MyTestId : System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..ba5c87baf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet6_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 + + readonly partial struct MyTestId : System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..ba5c87baf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.DotNet7_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 + + readonly partial struct MyTestId : System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..ba5c87baf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..ba5c87baf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IParsable.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 + + readonly partial struct MyTestId : System.IParsable + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..300f67897 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.Core3_1.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..300f67897 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.DotNet6_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..300f67897 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.DotNet7_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 + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..300f67897 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + 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_type=String_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.verified.txt index 0987c7d64..300f67897 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.verified.txt @@ -18,6 +18,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..7ca7adca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Core3_1.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..7ca7adca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet6_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..7ca7adca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.DotNet7_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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..7ca7adca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..7ca7adca5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SwaggerSchemaFilter_i=None.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 + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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 MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..eaf28933d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Core3_1.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..eaf28933d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet6_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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..eaf28933d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.DotNet7_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))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..eaf28933d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt index 1396ccae3..eaf28933d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..174e71a6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Core3_1.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..174e71a6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet6_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..174e71a6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.DotNet7_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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..174e71a6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.Net4_8.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public MyTestId() + { + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override 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); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt index d35ac20a3..174e71a6b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt @@ -19,6 +19,10 @@ Value = value ?? throw new System.ArgumentNullException(nameof(value)); } + public MyTestId() + { + } + public static readonly MyTestId Empty = new MyTestId(string.Empty); public bool Equals(MyTestId other) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.verified.txt new file mode 100644 index 000000000..43fb24c8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.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 MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_0.verified.txt new file mode 100644 index 000000000..43fb24c8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_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 MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_0.verified.txt new file mode 100644 index 000000000..43fb24c8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_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 MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt new file mode 100644 index 000000000..43fb24c8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt index ff61cdde7..43fb24c8f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt @@ -23,6 +23,10 @@ namespace MyTestNamespace Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Core3_1.verified.txt new file mode 100644 index 000000000..e87cb15bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Core3_1.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 MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_0.verified.txt new file mode 100644 index 000000000..e87cb15bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_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 MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_0.verified.txt new file mode 100644 index 000000000..e87cb15bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_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 MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt new file mode 100644 index 000000000..e87cb15bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt index dacf3158c..e87cb15bf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt @@ -25,6 +25,10 @@ namespace MyTestNamespace Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Core3_1.verified.txt new file mode 100644 index 000000000..9867823d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Core3_1.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 MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_0.verified.txt new file mode 100644 index 000000000..9867823d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_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 MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_0.verified.txt new file mode 100644 index 000000000..9867823d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_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 MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt new file mode 100644 index 000000000..9867823d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public MyTestId() + { + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => 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); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt index b71458e7e..9867823d0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt @@ -27,6 +27,10 @@ namespace MyTestNamespace Value = value; } + public MyTestId() + { + } + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..4b1b6e2eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Core3_1.verified.txt @@ -0,0 +1,335 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..4b1b6e2eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt @@ -0,0 +1,335 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..4b1b6e2eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt @@ -0,0 +1,335 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..4b1b6e2eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt @@ -0,0 +1,335 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt index e397007be..4b1b6e2eb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -238,6 +251,10 @@ namespace StronglyTypedIds Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt index 0b021b89a..b85dc5507 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace SomeNamespace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..b85dc5507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt index 0b021b89a..b85dc5507 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace SomeNamespace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt index 1be7dd3cb..ce6162e7c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt index a7e3384bc..f06762edb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt index e8ca99b42..80f2b5c33 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt index 62ccb4765..53c4a3a3e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt index af074ad91..816a481dd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..ce6162e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,310 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt index 1be7dd3cb..ce6162e7c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt new file mode 100644 index 000000000..f06762edb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt @@ -0,0 +1,296 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt index a7e3384bc..f06762edb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..80f2b5c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,289 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt index e8ca99b42..80f2b5c33 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt new file mode 100644 index 000000000..53c4a3a3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt @@ -0,0 +1,317 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt index 62ccb4765..53c4a3a3e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -239,6 +252,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt new file mode 100644 index 000000000..816a481dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt @@ -0,0 +1,338 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt index af074ad91..816a481dd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace MyTests.TestNameSpace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt new file mode 100644 index 000000000..a4df06dc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt @@ -0,0 +1,446 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt new file mode 100644 index 000000000..a4df06dc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt @@ -0,0 +1,446 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt new file mode 100644 index 000000000..a4df06dc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt @@ -0,0 +1,446 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt new file mode 100644 index 000000000..a4df06dc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt @@ -0,0 +1,446 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt index 7e73d73fe..a4df06dc5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -240,6 +253,10 @@ namespace MyContracts.V1 Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); @@ -344,6 +361,10 @@ namespace MyContracts.V2 Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..f3387af08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt @@ -0,0 +1,341 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..f3387af08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt @@ -0,0 +1,341 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..f3387af08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt @@ -0,0 +1,341 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..f3387af08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt @@ -0,0 +1,341 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt index c259d55ac..f3387af08 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -242,6 +255,10 @@ namespace SomeNamespace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..32b19a191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt @@ -0,0 +1,347 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..32b19a191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt @@ -0,0 +1,347 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..32b19a191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt @@ -0,0 +1,347 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..32b19a191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt @@ -0,0 +1,347 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public MyId() + { + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt index ebcdb6716..32b19a191 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -246,6 +259,10 @@ namespace SomeNamespace Value = value; } + public MyId() + { + } + public static MyId New() => new MyId(System.Guid.NewGuid()); public static readonly MyId Empty = new MyId(System.Guid.Empty); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..0c614944a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt @@ -0,0 +1,271 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public MyId() + { + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..0c614944a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt @@ -0,0 +1,271 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public MyId() + { + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..0c614944a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt @@ -0,0 +1,271 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public MyId() + { + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..0c614944a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt @@ -0,0 +1,271 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may 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 MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public MyId() + { + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt index 02e7314d4..0c614944a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt @@ -173,6 +173,12 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + } } #endif @@ -188,7 +194,7 @@ namespace StronglyTypedIds [Flags] internal enum StronglyTypedIdImplementations { - // Used with HasFlag, so needs to be 1, 2, 4 etc + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc /// /// Don't implement any additional members for the strongly typed ID @@ -213,8 +219,15 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8 } } + #endif //------------------------------------------------------------------------------ // @@ -236,6 +249,10 @@ namespace StronglyTypedIds Value = value; } + public MyId() + { + } + public static readonly MyId Empty = new MyId(0); public bool Equals(MyId other) => this.Value.Equals(other.Value); diff --git a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs index 383b9a129..b05237321 100644 --- a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs +++ b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs @@ -157,11 +157,11 @@ public static IEnumerable Parameters() } // All individual implementations - foreach (var implementation in EnumHelper.AllImplementations(includeDefault: false)) + foreach (var implementation in EnumHelper.AllImplementations(includeDefault: false).Where(a=> a != StronglyTypedIdImplementations.None)) { yield return new object[] { backingType, StronglyTypedIdConverter.None, implementation }; } } } } -} \ No newline at end of file +} diff --git a/test/StronglyTypedIds.Tests/StronglyTypedIdGeneratorTests.cs b/test/StronglyTypedIds.Tests/StronglyTypedIdGeneratorTests.cs index db399a465..287e7ed13 100644 --- a/test/StronglyTypedIds.Tests/StronglyTypedIdGeneratorTests.cs +++ b/test/StronglyTypedIds.Tests/StronglyTypedIdGeneratorTests.cs @@ -224,6 +224,7 @@ public static string ToArgument(StronglyTypedIdConverter converter) => StronglyTypedIdConverter.NewtonsoftJson => "StronglyTypedIdConverter.NewtonsoftJson", StronglyTypedIdConverter.SystemTextJson => "StronglyTypedIdConverter.SystemTextJson", StronglyTypedIdConverter.TypeConverter => "StronglyTypedIdConverter.TypeConverter", + StronglyTypedIdConverter.SwaggerSchemaFilter => "StronglyTypedIdConverter.SwaggerSchemaFilter", _ when converter.HasFlag(StronglyTypedIdConverter.NewtonsoftJson) && converter.HasFlag(StronglyTypedIdConverter.SystemTextJson) => "StronglyTypedIdConverter.NewtonsoftJson | StronglyTypedIdConverter.SystemTextJson", diff --git a/version.props b/version.props index 5ecc41198..8de93cda4 100644 --- a/version.props +++ b/version.props @@ -1,7 +1,7 @@ - 1.0.0 - beta06 + 1.0.19 + $(VersionPrefix) $(VersionPrefix)-$(VersionSuffix)