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..fd9ff408c 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs @@ -46,5 +46,21 @@ 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, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs index d0900fc4f..5d145ccb1 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,23 @@ public enum StronglyTypedIdImplementations /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdInterop.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdInterop.cs new file mode 100644 index 000000000..a04d247b2 --- /dev/null +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdInterop.cs @@ -0,0 +1,6 @@ +namespace StronglyTypedIds; + +public interface IInternalStronglyTypedId +{ + public T Value { get; } +} 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..ce11512c7 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,6 +25,12 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_LinqToDbTypeConverter.cs"), false ); @@ -37,6 +43,12 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_LinqToDbTypeConverter.cs"), false ); @@ -49,6 +61,12 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_LinqToDbTypeConverter.cs"), false ); @@ -61,6 +79,12 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_LinqToDbTypeConverter.cs"), false ); @@ -73,6 +97,12 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_LinqToDbTypeConverter.cs"), true ); @@ -85,12 +115,19 @@ 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_Convertible.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_StronglyTypedId.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_AutoMapperTypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_LinqToDbTypeConverter.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 +145,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; } @@ -116,7 +154,13 @@ public readonly struct ResourceCollection public string TypeConverter { get; } public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } + public string AutoMapperTypeHandler { get; } + public string LinqToDbTypeHandler { get; } public string Comparable { get; } + public string Parsable { get; } + public string Convertible { get; } + + public string StronglyTypedId { get; } public ResourceCollection( string header, @@ -127,16 +171,28 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, + string parsable, + string convertible, + string stronglyTypedId, + string swaggerSchemaFilter, + string autoMapperTypeHandler, + string linqToDbTypeHandler, bool nullableEnable) { + SwaggerSchemaFilter = swaggerSchemaFilter; BaseId = baseId; Newtonsoft = newtonsoft; SystemTextJson = systemTextJson; TypeConverter = typeConverter; EfCoreValueConverter = efCoreValueConverter; DapperTypeHandler = dapperTypeHandler; + AutoMapperTypeHandler = autoMapperTypeHandler; + LinqToDbTypeHandler = linqToDbTypeHandler; Comparable = comparable; + Parsable = parsable; + Convertible = convertible; NullableEnable = nullableEnable; + StronglyTypedId = stronglyTypedId; Header = header; } } @@ -153,4 +209,4 @@ internal static string LoadTemplateForEmitting(string resourceName) #endif"; } } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds/ParentClass.cs b/src/StronglyTypedIds/ParentClass.cs index 012c41274..093c7c0fb 100644 --- a/src/StronglyTypedIds/ParentClass.cs +++ b/src/StronglyTypedIds/ParentClass.cs @@ -1,3 +1,5 @@ +using System; + namespace StronglyTypedIds; internal class ParentClass @@ -14,4 +16,4 @@ public ParentClass(string keyword, string name, string constraints, ParentClass? public string Keyword { get; } public string Name { get; } public string Constraints { get; } -} \ No newline at end of file +} diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index e33515310..b64ada6df 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -35,7 +35,7 @@ public static string CreateId( _ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)), }; - return CreateId(idNamespace, idName, parentClass, converters, implementations, resources, sb); + return CreateId(idNamespace, idName, parentClass, converters, implementations, resources,backingType, sb); } static string CreateId( @@ -45,6 +45,7 @@ static string CreateId( StronglyTypedIdConverter converters, StronglyTypedIdImplementations implementations, EmbeddedSources.ResourceCollection resources, + StronglyTypedIdBackingType backingType, StringBuilder? sb) { if (string.IsNullOrEmpty(idName)) @@ -64,14 +65,20 @@ 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); var useEfCoreValueConverter = converters.IsSet(StronglyTypedIdConverter.EfCoreValueConverter); var useDapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.DapperTypeHandler); + var useAutoMapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.AutoMapper); + var useLinqToDbTypeHandler = converters.IsSet(StronglyTypedIdConverter.LinqToDb); var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable); var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable); + var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable); + var useIConvertible = implementations.IsSet(StronglyTypedIdImplementations.IConvertible); + var useIStronglyTypedId = implementations.IsSet(StronglyTypedIdImplementations.IStronglyTypedId); var parentsCount = 0; @@ -122,8 +129,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, useIConvertible, useIStronglyTypedId, backingType); // IEquatable is already implemented whether or not the interface is implemented @@ -132,6 +145,21 @@ static string CreateId( sb.AppendLine(resources.Comparable); } + if (useIParsable) + { + sb.AppendLine(resources.Parsable); + } + + if (useIConvertible) + { + sb.AppendLine(resources.Convertible); + } + + if (useIStronglyTypedId) + { + sb.AppendLine(resources.StronglyTypedId); + } + if (useEfCoreValueConverter) { sb.AppendLine(resources.EfCoreValueConverter); @@ -142,6 +170,16 @@ static string CreateId( sb.AppendLine(resources.DapperTypeHandler); } + if (useAutoMapperTypeHandler) + { + sb.AppendLine(resources.AutoMapperTypeHandler); + } + + if (useLinqToDbTypeHandler) + { + sb.AppendLine(resources.LinqToDbTypeHandler); + } + if (useTypeConverter) { sb.AppendLine(resources.TypeConverter); @@ -157,6 +195,11 @@ static string CreateId( sb.AppendLine(resources.SystemTextJson); } + if (useSchemaFilter) + { + sb.AppendLine(resources.SwaggerSchemaFilter); + } + sb.Replace("TESTID", idName); sb.AppendLine(@" }"); @@ -173,13 +216,30 @@ static string CreateId( return sb.ToString(); } - private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable) + + private static string BackingType(StronglyTypedIdBackingType backingType) + { + var resources = backingType switch + { + StronglyTypedIdBackingType.Guid => "System.Guid", + StronglyTypedIdBackingType.Int => "int", + StronglyTypedIdBackingType.Long => "long", + StronglyTypedIdBackingType.String => "string", + StronglyTypedIdBackingType.NullableString => "string?", + StronglyTypedIdBackingType.MassTransitNewId => "MassTransit.NewId", + _ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)), + }; + return resources; + } + + private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable, bool useIConvertible, bool useIStronglyTypedId, StronglyTypedIdBackingType backingType) { var interfaces = new List(); if (useIComparable) { interfaces.Add("System.IComparable"); + interfaces.Add("System.IComparable"); } if (useIEquatable) @@ -187,6 +247,23 @@ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool interfaces.Add("System.IEquatable"); } + if (useIParsable) + { + interfaces.Add("System.IParsable"); + } + + if (useIConvertible) + { + interfaces.Add("System.IConvertible"); + } + + if (useIStronglyTypedId) + { + interfaces.Add($"IStronglyTypedId<{BackingType(backingType)}>"); + } + + interfaces.Add($"StronglyTypedIds.IInternalStronglyTypedId<{BackingType(backingType)}>"); + 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Guid/Guid_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..88662e3ea --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public System.Guid Convert(TESTID source, System.Guid destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs index 200147a15..ad6c5cb12 100644 --- a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public System.Guid Value { get; } @@ -16,7 +17,6 @@ public override bool Equals(object obj) if (ReferenceEquals(null, obj)) return false; return obj is TESTID other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs new file mode 100644 index 000000000..56e2b009d --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Guid/Guid_DapperTypeHandler.cs index 7d94a235e..b81be3fa9 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 + } + +#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/Guid/Guid_IComparable.cs b/src/StronglyTypedIds/Templates/Guid/Guid_IComparable.cs index e0696ee2a..885fabb17 100644 --- a/src/StronglyTypedIds/Templates/Guid/Guid_IComparable.cs +++ b/src/StronglyTypedIds/Templates/Guid/Guid_IComparable.cs @@ -1 +1,17 @@ - public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/Guid/Guid_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..140561df8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_LinqToDbTypeConverter.cs @@ -0,0 +1,4 @@ + +#if !FAKE_CODE + +#endif diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs new file mode 100644 index 000000000..e875ffe05 --- /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) + { + Guid res = Guid.Empty; + var ok = Guid.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs new file mode 100644 index 000000000..5f234d181 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(System.Guid value) + { + return new TESTID(value); + } 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Int/Int_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..1dd7017e0 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(TESTID source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/Int/Int_Base.cs b/src/StronglyTypedIds/Templates/Int/Int_Base.cs index 08ce1fca6..9e2c2d30a 100644 --- a/src/StronglyTypedIds/Templates/Int/Int_Base.cs +++ b/src/StronglyTypedIds/Templates/Int/Int_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public int Value { get; } @@ -19,5 +20,6 @@ public override bool Equals(object obj) public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs b/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs new file mode 100644 index 000000000..699cd94e7 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + return Value; + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Int/Int_DapperTypeHandler.cs index 8e3dc42c9..31c7dec28 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,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/Int/Int_IComparable.cs b/src/StronglyTypedIds/Templates/Int/Int_IComparable.cs index e0696ee2a..50e5548ad 100644 --- a/src/StronglyTypedIds/Templates/Int/Int_IComparable.cs +++ b/src/StronglyTypedIds/Templates/Int/Int_IComparable.cs @@ -1 +1,17 @@ - public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/Int/Int_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/Int/Int_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..140561df8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_LinqToDbTypeConverter.cs @@ -0,0 +1,4 @@ + +#if !FAKE_CODE + +#endif 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_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Int/Int_StronglyTypedId.cs new file mode 100644 index 000000000..ba09088b8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(int value) + { + return new TESTID(value); + } 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/Long/Long_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..30ce72e23 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(TESTID source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/Long/Long_Base.cs b/src/StronglyTypedIds/Templates/Long/Long_Base.cs index 38d9fc5a6..ddf012d14 100644 --- a/src/StronglyTypedIds/Templates/Long/Long_Base.cs +++ b/src/StronglyTypedIds/Templates/Long/Long_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public long Value { get; } @@ -19,5 +20,6 @@ public override bool Equals(object obj) public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs b/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs new file mode 100644 index 000000000..db20ba2b2 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + return (decimal)Value; + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + return Value; + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs index 42dac85c9..12575aa72 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_IComparable.cs b/src/StronglyTypedIds/Templates/Long/Long_IComparable.cs index e0696ee2a..716d670d0 100644 --- a/src/StronglyTypedIds/Templates/Long/Long_IComparable.cs +++ b/src/StronglyTypedIds/Templates/Long/Long_IComparable.cs @@ -1 +1,17 @@ - public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/Long/Long_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/Long/Long_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..2ecaa2ff8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_LinqToDbTypeConverter.cs @@ -0,0 +1,13 @@ + +#if !FAKE_CODE +public static void UseLinqToDbConverter(LinqToDB.Mapping.MappingSchema mappingSchema) +{ + mappingSchema.SetConvertExpression(a => new LinqToDB.Data.DataParameter() { DataType = LinqToDB.DataType.Decimal, Value = a.Value}); + mappingSchema.SetConvertExpression(p => (long)p.Value); +} + +public static TESTID op_Implicit(decimal value) +{ + return new TESTID((long)value); +} +#endif 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_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/Long/Long_StronglyTypedId.cs new file mode 100644 index 000000000..e306b02be --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(long value) + { + return new TESTID(value); + } 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/NewId/NewId_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..c30881e0e --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(TESTID source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs index 854cd477a..510e3378c 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public MassTransit.NewId Value { get; } @@ -20,5 +21,6 @@ public override bool Equals(object obj) public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs b/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs new file mode 100644 index 000000000..56e2b009d --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs index 8d4af7b02..a5a3dc3a6 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_DapperTypeHandler.cs @@ -15,4 +15,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/NewId/NewId_IComparable.cs b/src/StronglyTypedIds/Templates/NewId/NewId_IComparable.cs index e0696ee2a..b174b5efd 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_IComparable.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_IComparable.cs @@ -1 +1,16 @@ - public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/NewId/NewId_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..140561df8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_LinqToDbTypeConverter.cs @@ -0,0 +1,4 @@ + +#if !FAKE_CODE + +#endif 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_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/NewId/NewId_StronglyTypedId.cs new file mode 100644 index 000000000..050a6d892 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(MassTransit.NewId value) + { + return new TESTID(value); + } 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..6b4ebc97e --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(TESTID source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs index 94a6eaffb..bcc1fc068 100644 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public string? Value { get; } @@ -27,5 +28,6 @@ public override bool Equals(object? obj) public override int GetHashCode() => Value?.GetHashCode() ?? 0; public override string? ToString() => Value; + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); public static bool operator !=(TESTID a, TESTID b) => !(a == b); diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs new file mode 100644 index 000000000..56e2b009d --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_DapperTypeHandler.cs index 1524a13b1..93064febd 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_IComparable.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_IComparable.cs index 5cdc51ac9..55d9526c2 100644 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_IComparable.cs +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_IComparable.cs @@ -1,4 +1,6 @@ - public int CompareTo(TESTID other) +#pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) { return (Value, other.Value) switch { @@ -7,4 +9,16 @@ (_, null) => 1, (_, _) => Value.CompareTo(other.Value), }; - } \ No newline at end of file + } + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..140561df8 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_LinqToDbTypeConverter.cs @@ -0,0 +1,4 @@ + +#if !FAKE_CODE + +#endif 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_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_StronglyTypedId.cs new file mode 100644 index 000000000..a347d496f --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(string? value) + { + return new TESTID(value); + } 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_AutoMapperTypeConverter.cs b/src/StronglyTypedIds/Templates/String/String_AutoMapperTypeConverter.cs new file mode 100644 index 000000000..f3ee1e17a --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_AutoMapperTypeConverter.cs @@ -0,0 +1,10 @@ + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(TESTID source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif diff --git a/src/StronglyTypedIds/Templates/String/String_Base.cs b/src/StronglyTypedIds/Templates/String/String_Base.cs index 023df780b..2e704c3f6 100644 --- a/src/StronglyTypedIds/Templates/String/String_Base.cs +++ b/src/StronglyTypedIds/Templates/String/String_Base.cs @@ -1,4 +1,5 @@ - readonly partial struct TESTID : INTERFACES + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct TESTID : INTERFACES { public string Value { get; } diff --git a/src/StronglyTypedIds/Templates/String/String_Convertible.cs b/src/StronglyTypedIds/Templates/String/String_Convertible.cs new file mode 100644 index 000000000..420abadc7 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_Convertible.cs @@ -0,0 +1,89 @@ + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + return Value.ToString(provider); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/String/String_DapperTypeHandler.cs index 93d6ff7bf..3fe8cdcff 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_IComparable.cs b/src/StronglyTypedIds/Templates/String/String_IComparable.cs index 5cdc51ac9..4b36cb899 100644 --- a/src/StronglyTypedIds/Templates/String/String_IComparable.cs +++ b/src/StronglyTypedIds/Templates/String/String_IComparable.cs @@ -1,4 +1,6 @@ - public int CompareTo(TESTID other) +#pragma warning disable CA1036 +#nullable enable + public int CompareTo(TESTID other) { return (Value, other.Value) switch { @@ -7,4 +9,17 @@ (_, null) => 1, (_, _) => Value.CompareTo(other.Value), }; - } \ No newline at end of file + } + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not TESTID other) + throw new System.ArgumentException("Object is not a TESTID"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 diff --git a/src/StronglyTypedIds/Templates/String/String_LinqToDbTypeConverter.cs b/src/StronglyTypedIds/Templates/String/String_LinqToDbTypeConverter.cs new file mode 100644 index 000000000..925a4aa01 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_LinqToDbTypeConverter.cs @@ -0,0 +1,14 @@ + +#if !FAKE_CODE +public static void UseLinqToDbConverter(LinqToDB.Mapping.MappingSchema mappingSchema) +{ + mappingSchema.SetConvertExpression(a => new LinqToDB.Data.DataParameter() { DataType = LinqToDB.DataType.VarChar, Value = a.Value}); + mappingSchema.SetConvertExpression(p => (string)p.Value); +} + +public static TESTID op_Implicit(string value) +{ + return new TESTID(value); +} + +#endif diff --git a/src/StronglyTypedIds/Templates/String/String_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/String/String_NewtonsoftJsonConverter.cs index e331c4bdc..58d9368c9 100644 --- a/src/StronglyTypedIds/Templates/String/String_NewtonsoftJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/String/String_NewtonsoftJsonConverter.cs @@ -16,11 +16,11 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type o { if (objectType == typeof(TESTID?)) { - var value = serializer.Deserialize(reader); + var value = serializer.Deserialize(reader); return value is null ? null : new TESTID(value); } return new TESTID(serializer.Deserialize(reader)); } - } \ No newline at end of file + } diff --git a/src/StronglyTypedIds/Templates/String/String_Parsable.cs b/src/StronglyTypedIds/Templates/String/String_Parsable.cs new file mode 100644 index 000000000..67502db03 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_Parsable.cs @@ -0,0 +1,21 @@ + +#nullable enable + public static TESTID Parse(string s, System.IFormatProvider? provider) + { + return new TESTID(s.ToString(provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result) + { + var ok = s != null; + if (ok) + { + result = new TESTID(s!.ToString(provider)); + } + else + { + result = new TESTID(""); + } + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs b/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs new file mode 100644 index 000000000..763971d65 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_StronglyTypedId.cs @@ -0,0 +1,5 @@ + + public static IStronglyTypedId Create(string value) + { + return new TESTID(value); + } 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..8156d1080 100644 --- a/test/IntegrationLibraries.props +++ b/test/IntegrationLibraries.props @@ -1,27 +1,16 @@ - + - - - - - - - - - - - - + - + - + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -32,4 +21,4 @@ all - \ No newline at end of file + diff --git a/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs b/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs new file mode 100644 index 000000000..58071d2e6 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/AutoMapper.cs @@ -0,0 +1,13 @@ +namespace AutoMapper; + +//FAKE STUB +public interface ITypeConverter +{ + +} + +//FAKE STUB +public class ResolutionContext +{ + +} 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/InternalStronglyTypedId.cs b/test/StronglyTypedIds.IntegrationTests/InternalStronglyTypedId.cs new file mode 100644 index 000000000..d61c55c03 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/InternalStronglyTypedId.cs @@ -0,0 +1,6 @@ +namespace StronglyTypedIds +{ + public interface IInternalStronglyTypedId + { + } +} diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs index b2f70dbcb..1f7ad4064 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; } @@ -350,4 +371,4 @@ public class EntityWithNullableId public NewtonsoftJsonLongId? Id { get; set; } } } -} \ No newline at end of file +} diff --git a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs index e6dce6d74..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/StronglyTypedId.cs b/test/StronglyTypedIds.IntegrationTests/StronglyTypedId.cs new file mode 100644 index 000000000..0fc83ff50 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedId.cs @@ -0,0 +1,3 @@ +//FAKE STUB +public interface IStronglyTypedId { +} diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj index 2800167d3..1717ab275 100644 --- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj @@ -1,16 +1,28 @@ - netcoreapp3.1;net6.0;net7.0 - net48;$(TargetFrameworks) + net7.0 false + disable true + + TRACE;FAKE_CODE + + + + TRACE;FAKE_CODE + + + + + + diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs index 9ad651d72..bfefdc655 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 { } @@ -37,4 +42,4 @@ public partial struct EquatableGuidId { } [StronglyTypedId(implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableGuidId { } -} \ No newline at end of file +} 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..78cbbb998 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs @@ -1,4 +1,4 @@ -using StronglyTypedIds; +using AutoMapper; namespace StronglyTypedIds.IntegrationTests.Types { @@ -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 { } @@ -34,4 +39,11 @@ public partial struct EquatableLongId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableLongId { } -} \ No newline at end of file + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IStronglyTypedId)] + public partial struct StronglyTypedIdLongId { } + + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, converters : StronglyTypedIdConverter.AutoMapper)] + public partial struct AutoMappedLongId { } +} 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..61394e984 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -1,8 +1,7 @@ - netcoreapp3.1;net6.0;net7.0 - net48;$(TargetFrameworks) + net7.0 false STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -17,7 +16,7 @@ - + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index 38b3c43f8..58130c1d1 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -1,8 +1,7 @@ - netcoreapp3.1;net6.0;net7.0 - net48;$(TargetFrameworks) + net7.0 false @@ -16,7 +15,7 @@ - + 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..e78ef8c7c 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,23 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #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..485917d76 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,25 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif \ No newline at end of file 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..d49c00f77 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -30,14 +32,142 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + +#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) + { + Guid res = Guid.Empty; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(System.Guid value) + { + return new MyTestId(value); + } + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -54,6 +184,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) @@ -67,6 +198,31 @@ } } +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE + +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +296,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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt index e7affbacb..fe34efc09 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public int Value { get; } @@ -33,9 +35,139 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + +#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 + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + return Value; + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(int value) + { + return new MyTestId(value); + } + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { @@ -53,6 +185,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) @@ -67,6 +200,31 @@ } } +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE + +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +298,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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt index 0a464e952..21d5837f9 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public long Value { get; } @@ -33,9 +35,139 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 + + +#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 + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + return (decimal)Value; + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + return Value; + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(long value) + { + return new MyTestId(value); + } + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { @@ -53,6 +185,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,6 +194,7 @@ { 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"), @@ -68,6 +202,40 @@ } } +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE +public static void UseLinqToDbConverter(LinqToDB.Mapping.MappingSchema mappingSchema) +{ + mappingSchema.SetConvertExpression(a => new LinqToDB.Data.DataParameter() { DataType = LinqToDB.DataType.Decimal, Value = a.Value}); + mappingSchema.SetConvertExpression(p => (long)p.Value); +} + +public static MyTestId op_Implicit(decimal value) +{ + return new MyTestId((long)value); +} +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -143,4 +311,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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt index c2c044246..f3c950cbd 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public MassTransit.NewId Value { get; } @@ -34,9 +36,136 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + + 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; + } + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(MassTransit.NewId value) + { + return new MyTestId(value); + } + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { @@ -67,6 +196,32 @@ } } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE + +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -148,4 +303,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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt index 975c29ade..426f9f0fd 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public string? Value { get; } @@ -42,8 +44,11 @@ public override int GetHashCode() => Value?.GetHashCode() ?? 0; public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); +#pragma warning disable CA1036 +#nullable enable public int CompareTo(MyTestId other) { return (Value, other.Value) switch @@ -54,6 +59,127 @@ (_, _) => Value.CompareTo(other.Value), }; } + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 + + + 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; + } + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(string? value) + { + return new MyTestId(value); + } + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { @@ -71,6 +197,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) @@ -85,6 +212,31 @@ } } +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE + +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) @@ -172,4 +324,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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt index 3e93ee6b2..4d2ae2fb8 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,9 @@ [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))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { public string Value { get; } @@ -44,6 +46,8 @@ public override string ToString() => Value; public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); +#pragma warning disable CA1036 +#nullable enable public int CompareTo(MyTestId other) { return (Value, other.Value) switch @@ -55,6 +59,138 @@ }; } + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable +#pragma warning restore CA1036 + + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(s.ToString(provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + var ok = s != null; + if (ok) + { + result = new MyTestId(s!.ToString(provider)); + } + else + { + result = new MyTestId(""); + } + return ok; + } +#nullable disable + + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + return Value.ToString(provider); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + + public static IStronglyTypedId Create(string value) + { + return new MyTestId(value); + } + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } @@ -71,6 +207,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,6 +220,41 @@ } } +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + +#if !FAKE_CODE +public static void UseLinqToDbConverter(LinqToDB.Mapping.MappingSchema mappingSchema) +{ + mappingSchema.SetConvertExpression(a => new LinqToDB.Data.DataParameter() { DataType = LinqToDB.DataType.VarChar, Value = a.Value}); + mappingSchema.SetConvertExpression(p => (string)p.Value); +} + +public static MyTestId op_Implicit(string value) +{ + return new MyTestId(value); +} + +#endif + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -137,7 +309,7 @@ { if (objectType == typeof(MyTestId?)) { - var value = serializer.Deserialize(reader); + var value = serializer.Deserialize(reader); return value is null ? null : new MyTestId(value); } @@ -146,6 +318,7 @@ } } + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) @@ -158,4 +331,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=Long_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.verified.txt similarity index 59% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.verified.txt index cb5754863..00427a84a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.verified.txt @@ -9,16 +9,18 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { - public long Value { get; } + public System.Guid Value { get; } - public MyTestId(long value) + public MyTestId(System.Guid value) { Value = value; } - public static readonly MyTestId Empty = new MyTestId(0); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -26,21 +28,20 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter { - public EfCoreValueConverter() : this(null) { } - public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) - : base( - id => id.Value, - value => new MyTestId(value), - mappingHints - ) { } + public System.Guid Convert(MyTestId source, System.Guid destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } } +#endif + } 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..7c7bf4556 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 @@ -9,7 +9,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -27,7 +28,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); @@ -39,6 +39,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) @@ -51,4 +52,13 @@ }; } } + +#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=Guid_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.verified.txt index 508208c37..997811716 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 @@ -9,7 +9,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -27,7 +28,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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=Guid_c=LinqToDb_i=None.verified.txt similarity index 69% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=LinqToDb_i=None.verified.txt index 45272d34a..4b72a0605 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=LinqToDb_i=None.verified.txt @@ -9,16 +9,18 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { - public int Value { get; } + public System.Guid Value { get; } - public MyTestId(int value) + public MyTestId(System.Guid value) { Value = value; } - public static readonly MyTestId Empty = new MyTestId(0); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -26,10 +28,14 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#if !FAKE_CODE + +#endif + } 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..c1a889193 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 @@ -10,7 +10,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -28,7 +29,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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..fd4f03be6 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 @@ -9,7 +9,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId : System.IComparable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IComparable, System.IComparable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -27,11 +28,27 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyTestId other) + throw new System.ArgumentException("Object is not a MyTestId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.verified.txt new file mode 100644 index 000000000..1f633f73a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.verified.txt @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IConvertible, StronglyTypedIds.IInternalStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +//ICONVERTIBLE +#nullable enable + + public System.TypeCode GetTypeCode() + { + throw new System.NotImplementedException(); + } + + public bool ToBoolean(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public byte ToByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public char ToChar(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public System.DateTime ToDateTime(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public decimal ToDecimal(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public double ToDouble(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public short ToInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public int ToInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public long ToInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public sbyte ToSByte(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public float ToSingle(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public string ToString(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public object ToType(System.Type conversionType, System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ushort ToUInt16(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public uint ToUInt32(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } + + public ulong ToUInt64(System.IFormatProvider? provider) + { + throw new System.NotImplementedException(); + } +#nullable disable + + } 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..c13c8b4f2 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 @@ -9,7 +9,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId : System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -27,7 +28,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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=Guid_c=None_i=IParsable.verified.txt similarity index 54% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt index 7c016c3be..d3876decd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt @@ -9,17 +9,18 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : System.IParsable, StronglyTypedIds.IInternalStronglyTypedId { - public int Value { get; } + public System.Guid Value { get; } - public MyTestId(int value) + public MyTestId(System.Guid value) { Value = value; } - public static readonly MyTestId Empty = new MyTestId(0); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -27,23 +28,25 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => 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 +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) { - public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - return new MyTestId(reader.GetInt32()); - } - - public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) - { - writer.WriteNumberValue(value.Value); - } + return new MyTestId(Guid.Parse(s, provider)); } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + Guid res = Guid.Empty; + 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=Long_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.verified.txt similarity index 63% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.verified.txt index c38fa63f6..ef0f9740d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.verified.txt @@ -9,16 +9,18 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId : System.IComparable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : IStronglyTypedId, StronglyTypedIds.IInternalStronglyTypedId { - public long Value { get; } + public System.Guid Value { get; } - public MyTestId(long value) + public MyTestId(System.Guid value) { Value = value; } - public static readonly MyTestId Empty = new MyTestId(0); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -26,11 +28,15 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static IStronglyTypedId Create(System.Guid value) + { + return new MyTestId(value); + } + } 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..cde95fe5a 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 @@ -9,7 +9,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -27,7 +28,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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=Guid_c=SwaggerSchemaFilter_i=None.verified.txt similarity index 50% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt index f8d24bbf5..0e54ec569 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt @@ -9,16 +9,19 @@ #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyTestId + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { - public int Value { get; } + public System.Guid Value { get; } - public MyTestId(int value) + public MyTestId(System.Guid value) { Value = value; } - public static readonly MyTestId Empty = new MyTestId(0); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -26,29 +29,22 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter { - public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) - { - parameter.Value = value.Value; - } - - public override MyTestId Parse(object value) + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) { - return value switch - { - int intValue => new MyTestId(intValue), - long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), - _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), - }; + 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.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.verified.txt index d6d801a99..326a24748 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 @@ -10,7 +10,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -28,7 +29,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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..c123a77a2 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 @@ -10,7 +10,8 @@ #pragma warning disable 1591 // publicly visible type or member must be documented [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -28,7 +29,6 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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 deleted file mode 100644 index d3e50467b..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt +++ /dev/null @@ -1,46 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public int Value { get; } - - public MyTestId(int value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.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=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt deleted file mode 100644 index b4d9738e7..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId - { - public int Value { get; } - - public MyTestId(int value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter - { - public override bool CanConvert(System.Type objectType) - { - return objectType == typeof(MyTestId); - } - - public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) - { - var id = (MyTestId)value; - serializer.Serialize(writer, id.Value); - } - - public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.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=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt deleted file mode 100644 index d76d3afd2..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt +++ /dev/null @@ -1,36 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IComparable - { - public int Value { get; } - - public MyTestId(int value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); - } 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 deleted file mode 100644 index 5d23b64a1..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt +++ /dev/null @@ -1,35 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IEquatable - { - public int Value { get; } - - public MyTestId(int value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 39f1e9331..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt +++ /dev/null @@ -1,77 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId - { - public int Value { get; } - - public MyTestId(int value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdTypeConverter : System.ComponentModel.TypeConverter - { - public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); - } - - public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - return value switch - { - int intValue => new MyTestId(intValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), - _ => base.ConvertFrom(context, culture, value), - }; - } - - public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); - } - - public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) - { - if (value is 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=Long_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt deleted file mode 100644 index 35fd482b5..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt +++ /dev/null @@ -1,55 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler - { - public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) - { - parameter.Value = value.Value; - } - - public override MyTestId Parse(object value) - { - return value switch - { - long longValue => new MyTestId(longValue), - int intValue => new MyTestId(intValue), - short shortValue => new MyTestId(shortValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), - _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), - }; - } - } - } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt deleted file mode 100644 index 7052a3b27..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter - { - public override bool CanConvert(System.Type objectType) - { - return objectType == typeof(MyTestId); - } - - public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) - { - var id = (MyTestId)value; - serializer.Serialize(writer, id.Value); - } - - public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.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=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt deleted file mode 100644 index 13b9b1ac7..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt +++ /dev/null @@ -1,35 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IEquatable - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index af1658c35..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt +++ /dev/null @@ -1,35 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 17a45333f..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt +++ /dev/null @@ -1,49 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter - { - public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - return new MyTestId(reader.GetInt64()); - } - - public override void Write(System.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=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt deleted file mode 100644 index e22d0478c..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt +++ /dev/null @@ -1,79 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId - { - public long Value { get; } - - public MyTestId(long value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(0); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdTypeConverter : System.ComponentModel.TypeConverter - { - public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); - } - - public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - return value switch - { - long longValue => new MyTestId(longValue), - int intValue => new MyTestId(intValue), - short shortValue => new MyTestId(shortValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), - _ => base.ConvertFrom(context, culture, value), - }; - } - - public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); - } - - public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) - { - if (value is 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=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt deleted file mode 100644 index 23efe4b09..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt +++ /dev/null @@ -1,54 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler - { - public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) - { - parameter.Value = value.Value.ToGuid(); - } - - public override MyTestId Parse(object value) - { - return value switch - { - System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), - string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), - _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), - }; - } - } - } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt deleted file mode 100644 index 9114f2d54..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt +++ /dev/null @@ -1,47 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter - { - public EfCoreValueConverter() : 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=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt deleted file mode 100644 index 181d85e44..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt +++ /dev/null @@ -1,57 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter - { - public override bool CanConvert(System.Type objectType) - { - return objectType == typeof(MyTestId); - } - - public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) - { - var id = (MyTestId)value; - serializer.Serialize(writer, id.Value.ToGuid()); - } - - public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.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=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt deleted file mode 100644 index 3a19fc65e..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt +++ /dev/null @@ -1,37 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IComparable - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); - } 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 deleted file mode 100644 index b4c2d5867..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt +++ /dev/null @@ -1,36 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IEquatable - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 7e4892f41..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt +++ /dev/null @@ -1,36 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 3de0600ab..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt +++ /dev/null @@ -1,50 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter - { - public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); - } - - public override void Write(System.Text.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=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt deleted file mode 100644 index 757eb4ebb..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt +++ /dev/null @@ -1,86 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId - { - public MassTransit.NewId Value { get; } - - public MyTestId(MassTransit.NewId value) - { - Value = value; - } - - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); - - public bool Equals(MyTestId other) => this.Value.Equals(other.Value); - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value.ToString(); - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdTypeConverter : System.ComponentModel.TypeConverter - { - public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || - sourceType == typeof(string) || base.CanConvertFrom - (context, sourceType); - } - - public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - return value switch - { - MassTransit.NewId newIdValue => new MyTestId(newIdValue), - System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), - string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), - _ => base.ConvertFrom(context, culture, value), - }; - } - - public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); - } - - public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) - { - if (value is MyTestId idValue) - { - if (destinationType == typeof(MassTransit.NewId)) - { - return idValue.Value; - } - - if (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=NullableString_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt deleted file mode 100644 index c96f1e168..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=DapperTypeHandler_i=None.verified.txt +++ /dev/null @@ -1,63 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler - { - public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) - { - parameter.Value = value.Value; - } - - public override MyTestId Parse(object value) - { - return value switch - { - null => new MyTestId(null), - System.DBNull => new MyTestId(null), - string stringValue => new MyTestId(stringValue), - _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), - }; - } - } - } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt deleted file mode 100644 index e1a174104..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=EfCoreValueConverter_i=None.verified.txt +++ /dev/null @@ -1,55 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.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=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt deleted file mode 100644 index 9660c1fc0..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=NewtonsoftJson_i=None.verified.txt +++ /dev/null @@ -1,71 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter - { - public override bool CanConvert(System.Type objectType) - { - return objectType == typeof(MyTestId); - } - - public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) - { - if (value is null) - { - serializer.Serialize(writer, null); - } - else - { - var id = (MyTestId)value; - serializer.Serialize(writer, id.Value); - } - } - - public override object ReadJson(Newtonsoft.Json.JsonReader reader, 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=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt deleted file mode 100644 index d779d1af4..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IComparable.verified.txt +++ /dev/null @@ -1,54 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - readonly partial struct MyTestId : System.IComparable - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => 0, - (null, _) => -1, - (_, null) => 1, - (_, _) => Value.CompareTo(other.Value), - }; - } - } 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 deleted file mode 100644 index bb2e2c7d2..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=IEquatable.verified.txt +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - readonly partial struct MyTestId : System.IEquatable - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index fe1e43665..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=None_i=None.verified.txt +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index c049fb7f5..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=SystemTextJson_i=None.verified.txt +++ /dev/null @@ -1,65 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter - { - public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - return new MyTestId(reader.GetString()); - } - - public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.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=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt deleted file mode 100644 index 50b3c0585..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=NullableString_c=TypeConverter_i=None.verified.txt +++ /dev/null @@ -1,87 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - -#nullable enable - [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId - { - public string? Value { get; } - - public MyTestId(string? value) - { - Value = value; - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value?.GetHashCode() ?? 0; - public override string? ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdTypeConverter : System.ComponentModel.TypeConverter - { - public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) - { - return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); - } - - public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) - { - if (value is null) - { - return new MyTestId(null); - } - - var stringValue = value as string; - if (stringValue is not null) - { - return new MyTestId(stringValue); - } - - return base.ConvertFrom(context, culture, value); - } - - public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) - { - return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); - } - - public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, 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=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt deleted file mode 100644 index 749dd1dbd..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt +++ /dev/null @@ -1,61 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler - { - public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) - { - parameter.Value = value.Value; - } - - public override MyTestId Parse(object value) - { - return value switch - { - string stringValue => new MyTestId(stringValue), - _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), - }; - } - } - } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt deleted file mode 100644 index 05700f78d..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt +++ /dev/null @@ -1,55 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.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=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt deleted file mode 100644 index d7596bf2b..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt +++ /dev/null @@ -1,71 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter - { - public override bool CanConvert(System.Type objectType) - { - return objectType == typeof(MyTestId); - } - - public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) - { - var id = (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=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt deleted file mode 100644 index 4fae5139a..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IComparable.verified.txt +++ /dev/null @@ -1,54 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IComparable - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => 0, - (null, _) => -1, - (_, null) => 1, - (_, _) => Value.CompareTo(other.Value), - }; - } - } 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 deleted file mode 100644 index 050bcc20b..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=IEquatable.verified.txt +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId : System.IEquatable - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 0987c7d64..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=None_i=None.verified.txt +++ /dev/null @@ -1,44 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } 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 deleted file mode 100644 index 1396ccae3..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt +++ /dev/null @@ -1,58 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter - { - public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) - { - return new MyTestId(reader.GetString()); - } - - public override void Write(System.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=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt deleted file mode 100644 index d35ac20a3..000000000 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt +++ /dev/null @@ -1,82 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by the StronglyTypedId source generator -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -#pragma warning disable 1591 // publicly visible type or member must be documented - - [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId - { - public string Value { get; } - - public MyTestId(string value) - { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); - } - - public static readonly MyTestId Empty = new MyTestId(string.Empty); - - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is MyTestId other && Equals(other); - } - - public override int GetHashCode() => Value.GetHashCode(); - - public override string ToString() => Value; - public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); - public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - - class MyTestIdTypeConverter : System.ComponentModel.TypeConverter - { - public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); - } - - public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) - { - var stringValue = value as string; - if (stringValue is not null) - { - return new MyTestId(stringValue); - } - - return base.ConvertFrom(context, culture, value); - } - - public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) - { - return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); - } - - public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, 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.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt index ff61cdde7..6a507efc4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt @@ -14,7 +14,8 @@ namespace MyTestNamespace partial record InnerMost { [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -32,7 +33,6 @@ namespace MyTestNamespace if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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..fd3ac54f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt @@ -16,7 +16,8 @@ namespace MyTestNamespace partial record InnerMost { [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -34,7 +35,6 @@ namespace MyTestNamespace if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); 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..9145e8044 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt @@ -18,7 +18,8 @@ namespace MyTestNamespace partial record InnerMost { [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] - readonly partial struct MyTestId + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyTestId : StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -36,7 +37,6 @@ namespace MyTestNamespace if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt index e397007be..9803bdd87 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -229,7 +265,8 @@ namespace StronglyTypedIds [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -247,14 +284,30 @@ namespace StronglyTypedIds 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt index 0b021b89a..757240217 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace SomeNamespace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace SomeNamespace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt index 0b021b89a..757240217 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace SomeNamespace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace SomeNamespace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) 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..4ce0a6906 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) 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..44abb3b40 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) 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..57ce10b15 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + 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) 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..4f01af0a1 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) 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..e329ae78d 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) 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..4ce0a6906 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) 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..44abb3b40 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) 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..57ce10b15 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + 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) 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..4f01af0a1 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -230,7 +266,8 @@ namespace StronglyTypedIds namespace MyTests.TestNameSpace { [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -248,14 +285,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) 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..e329ae78d 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,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace MyTests.TestNameSpace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace MyTests.TestNameSpace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt index 7e73d73fe..08cc10701 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -231,7 +267,8 @@ namespace MyContracts.V1 { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -249,14 +286,30 @@ namespace MyContracts.V1 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -335,7 +388,8 @@ namespace MyContracts.V2 { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -353,14 +407,30 @@ namespace MyContracts.V2 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt index c259d55ac..d722025a8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -233,7 +269,8 @@ namespace SomeNamespace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -251,14 +288,30 @@ namespace SomeNamespace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt index ebcdb6716..0c367703d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -237,7 +273,8 @@ namespace SomeNamespace { [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public System.Guid Value { get; } @@ -255,14 +292,30 @@ namespace SomeNamespace 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); + + #pragma warning disable CA1036 +#nullable enable public int CompareTo(MyId other) => Value.CompareTo(other.Value); + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + + class MyIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt index 02e7314d4..dfa041c50 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt @@ -173,8 +173,25 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + /// + /// Creates a LinqToDb bidirectional converters + /// + LinqToDb = 256, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +205,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 +230,27 @@ namespace StronglyTypedIds /// Implement the interface /// IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 } } + #endif //------------------------------------------------------------------------------ // @@ -227,7 +263,8 @@ namespace StronglyTypedIds #pragma warning disable 1591 // publicly visible type or member must be documented - readonly partial struct MyId : System.IComparable, System.IEquatable + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1036:Override methods on comparable types")] + readonly partial struct MyId : System.IComparable, System.IComparable, System.IEquatable, StronglyTypedIds.IInternalStronglyTypedId { public int Value { get; } @@ -248,7 +285,25 @@ namespace StronglyTypedIds 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); + + #pragma warning disable CA1036 +#nullable enable + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + public int CompareTo(object? obj) + { + if (ReferenceEquals(obj, null)) + return 1; + + if (obj is not MyId other) + throw new System.ArgumentException("Object is not a MyId"); + + return Value.CompareTo(other.Value); + } +#nullable disable + #pragma warning restore CA1036 + } diff --git a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs index 383b9a129..1efdb2dfb 100644 --- a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs +++ b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs @@ -95,7 +95,7 @@ public Task GeneratesFullIdCorrectly(StronglyTypedIdBackingType type) // combine them all var combinedConverter = EnumHelper.AllConverters(includeDefault: false) .Aggregate(StronglyTypedIdConverter.None, (prev, current) => prev | current); - + // combine them all var combinedImplementation = EnumHelper.AllImplementations(includeDefault: false) .Aggregate(StronglyTypedIdImplementations.None, (prev, current) => prev | current); @@ -148,7 +148,7 @@ public static IEnumerable BackingTypes() public static IEnumerable Parameters() { - foreach (var backingType in EnumHelper.AllBackingTypes(includeDefault: false)) + foreach (var backingType in EnumHelper.AllBackingTypes(includeDefault: false).Take(1)) { // All individual convert types foreach (var converter in EnumHelper.AllConverters(includeDefault: false)) @@ -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/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj b/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj index 4fc7f0fb2..6426603c6 100644 --- a/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj +++ b/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj @@ -1,8 +1,7 @@  - netcoreapp3.1;net6.0;net7.0 - net48;$(TargetFrameworks) + net7.0 false latest @@ -11,6 +10,8 @@ + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/StronglyTypedIds.Tests/Stub/AutoMapper.cs b/test/StronglyTypedIds.Tests/Stub/AutoMapper.cs new file mode 100644 index 000000000..58071d2e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Stub/AutoMapper.cs @@ -0,0 +1,13 @@ +namespace AutoMapper; + +//FAKE STUB +public interface ITypeConverter +{ + +} + +//FAKE STUB +public class ResolutionContext +{ + +} diff --git a/test/StronglyTypedIds.Tests/Stub/InternalStronglyTypedId.cs b/test/StronglyTypedIds.Tests/Stub/InternalStronglyTypedId.cs new file mode 100644 index 000000000..d61c55c03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Stub/InternalStronglyTypedId.cs @@ -0,0 +1,6 @@ +namespace StronglyTypedIds +{ + public interface IInternalStronglyTypedId + { + } +} diff --git a/test/StronglyTypedIds.Tests/Stub/StronglyTypedId.cs b/test/StronglyTypedIds.Tests/Stub/StronglyTypedId.cs new file mode 100644 index 000000000..d59f89274 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Stub/StronglyTypedId.cs @@ -0,0 +1,2 @@ +public interface IStronglyTypedId { +} diff --git a/version.props b/version.props index 5ecc41198..d40b75a65 100644 --- a/version.props +++ b/version.props @@ -1,7 +1,7 @@ - 1.0.0 - beta06 + 1.1.31 + $(VersionPrefix) $(VersionPrefix)-$(VersionSuffix)