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..a74f243e5 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs @@ -46,5 +46,16 @@ 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, + } -} \ 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/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..3640444a8 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,6 +25,11 @@ 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"), false ); @@ -37,6 +42,11 @@ 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"), false ); @@ -49,6 +59,11 @@ 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"), false ); @@ -61,6 +76,11 @@ 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"), false ); @@ -73,6 +93,11 @@ 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"), true ); @@ -85,12 +110,18 @@ 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"), 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 +139,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 +148,12 @@ public readonly struct ResourceCollection public string TypeConverter { get; } public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } + public string AutoMapperTypeHandler { get; } public string Comparable { get; } + public string Parsable { get; } + public string Convertible { get; } + + public string StronglyTypedId { get; } public ResourceCollection( string header, @@ -127,16 +164,26 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, + string parsable, + string convertible, + string stronglyTypedId, + string swaggerSchemaFilter, + string autoMapperTypeHandler, bool nullableEnable) { + SwaggerSchemaFilter = swaggerSchemaFilter; BaseId = baseId; Newtonsoft = newtonsoft; SystemTextJson = systemTextJson; TypeConverter = typeConverter; EfCoreValueConverter = efCoreValueConverter; DapperTypeHandler = dapperTypeHandler; + AutoMapperTypeHandler = autoMapperTypeHandler; Comparable = comparable; + Parsable = parsable; + Convertible = convertible; NullableEnable = nullableEnable; + StronglyTypedId = stronglyTypedId; Header = header; } } @@ -153,4 +200,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..8702cb641 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,19 @@ 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 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 +128,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 +144,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 +169,11 @@ static string CreateId( sb.AppendLine(resources.DapperTypeHandler); } + if (useAutoMapperTypeHandler) + { + sb.AppendLine(resources.AutoMapperTypeHandler); + } + if (useTypeConverter) { sb.AppendLine(resources.TypeConverter); @@ -157,6 +189,11 @@ static string CreateId( sb.AppendLine(resources.SystemTextJson); } + if (useSchemaFilter) + { + sb.AppendLine(resources.SwaggerSchemaFilter); + } + sb.Replace("TESTID", idName); sb.AppendLine(@" }"); @@ -173,7 +210,23 @@ 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(); @@ -187,6 +240,21 @@ 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)}>"); + } + 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..324fda87a 100644 --- a/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Base.cs @@ -16,7 +16,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_Parsable.cs b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs new file mode 100644 index 000000000..85f7095a7 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_Parsable.cs @@ -0,0 +1,15 @@ + +#nullable enable + public static TESTID Parse(string s, System.IFormatProvider? provider) + { + return new TESTID(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new TESTID(res); + return ok; + } +#nullable disable diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_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..71535f568 100644 --- a/src/StronglyTypedIds/Templates/Int/Int_Base.cs +++ b/src/StronglyTypedIds/Templates/Int/Int_Base.cs @@ -19,5 +19,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_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..772634aba 100644 --- a/src/StronglyTypedIds/Templates/Long/Long_Base.cs +++ b/src/StronglyTypedIds/Templates/Long/Long_Base.cs @@ -19,5 +19,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..5b176b826 --- /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) + { + 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) + { + 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_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..e7f3d668d 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_Base.cs @@ -20,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/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_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..7c91eed8e 100644 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs @@ -27,5 +27,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_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_Convertible.cs b/src/StronglyTypedIds/Templates/String/String_Convertible.cs new file mode 100644 index 000000000..56e2b009d --- /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) + { + 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/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_Parsable.cs b/src/StronglyTypedIds/Templates/String/String_Parsable.cs new file mode 100644 index 000000000..769bd1bb2 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_Parsable.cs @@ -0,0 +1,11 @@ + + public static TESTID Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result) + { + result = new TESTID(s); + return true; + } diff --git a/src/StronglyTypedIds/Templates/String/String_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..f4fd1dc04 100644 --- a/test/IntegrationLibraries.props +++ b/test/IntegrationLibraries.props @@ -3,12 +3,14 @@ + + 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/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..0efc8da6b 100644 --- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj @@ -2,15 +2,29 @@ netcoreapp3.1;net6.0;net7.0 - net48;$(TargetFrameworks) + $(TargetFrameworks) 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..17a628451 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -17,7 +17,7 @@ - + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index 38b3c43f8..4747ca652 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -16,7 +16,7 @@ - + diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Core3_1.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet6_0.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.DotNet7_0.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..58a2c4794 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdAttribute.Net4_8.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Core3_1.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet6_0.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.DotNet7_0.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt new file mode 100644 index 000000000..4ea1be319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.Net4_8.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt new file mode 100644 index 000000000..ac13b4a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Core3_1.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..ac13b4a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet6_0.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..ac13b4a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.DotNet7_0.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt new file mode 100644 index 000000000..ac13b4a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.Net4_8.verified.txt @@ -0,0 +1,76 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt index 32b324616..ac13b4a2e 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,18 @@ 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, + } } + #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Core3_1.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet6_0.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.DotNet7_0.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..3df9e2e31 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdDefaultsAttribute.Net4_8.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt new file mode 100644 index 000000000..485917d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Core3_1.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // 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/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt new file mode 100644 index 000000000..485917d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet6_0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // 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/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt new file mode 100644 index 000000000..485917d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.DotNet7_0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // 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/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt new file mode 100644 index 000000000..485917d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdImplementations.Net4_8.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // 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/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.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt new file mode 100644 index 000000000..1f599b78e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Core3_1.verified.txt @@ -0,0 +1,289 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#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 + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt new file mode 100644 index 000000000..1f599b78e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet6_0.verified.txt @@ -0,0 +1,289 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#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 + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt new file mode 100644 index 000000000..1f599b78e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.DotNet7_0.verified.txt @@ -0,0 +1,289 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#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 + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt new file mode 100644 index 000000000..1f599b78e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.Net4_8.verified.txt @@ -0,0 +1,289 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#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 + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt index d7a3ed3e7..1f599b78e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Guid.verified.txt @@ -12,7 +12,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public System.Guid Value { get; } @@ -30,7 +31,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(); @@ -38,6 +38,118 @@ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 +166,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 +180,26 @@ } } +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +273,17 @@ writer.WriteStringValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt new file mode 100644 index 000000000..f678704f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Core3_1.verified.txt @@ -0,0 +1,291 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(int)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt32()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt new file mode 100644 index 000000000..f678704f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet6_0.verified.txt @@ -0,0 +1,291 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(int)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt32()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt new file mode 100644 index 000000000..f678704f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.DotNet7_0.verified.txt @@ -0,0 +1,291 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(int)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt32()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt new file mode 100644 index 000000000..f678704f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.Net4_8.verified.txt @@ -0,0 +1,291 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public int Value { get; } + + public MyTestId(int value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int32; + } + + public override MyTestId Parse(object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public int Convert(MyTestId source, int destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + int intValue => new MyTestId(intValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(int)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt32()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt index e7affbacb..f678704f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Int.verified.txt @@ -12,7 +12,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public int Value { get; } @@ -33,10 +34,123 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(int.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + int res = 0; + var ok = int.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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 { public EfCoreValueConverter() : this(null) { } @@ -53,6 +167,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 +182,26 @@ } } +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -140,4 +275,17 @@ writer.WriteNumberValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int32"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt new file mode 100644 index 000000000..1f1b957f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Core3_1.verified.txt @@ -0,0 +1,295 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) + { + 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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(long)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt64()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt new file mode 100644 index 000000000..1f1b957f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet6_0.verified.txt @@ -0,0 +1,295 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) + { + 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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(long)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt64()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt new file mode 100644 index 000000000..1f1b957f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.DotNet7_0.verified.txt @@ -0,0 +1,295 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) + { + 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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(long)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt64()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt new file mode 100644 index 000000000..1f1b957f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.Net4_8.verified.txt @@ -0,0 +1,295 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public long Value { get; } + + public MyTestId(long value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(0); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) + { + 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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Int64; + } + + public override MyTestId Parse(object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + decimal decimalValue => new MyTestId((long)decimalValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public long Convert(MyTestId source, long destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + long longValue => new MyTestId(longValue), + int intValue => new MyTestId(intValue), + short shortValue => new MyTestId(shortValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(long)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetInt64()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt index 0a464e952..1f1b957f7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=Long.verified.txt @@ -12,7 +12,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public long Value { get; } @@ -33,10 +34,123 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(long.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = long.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + +//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) + { + 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 { public EfCoreValueConverter() : this(null) { } @@ -53,6 +167,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 +176,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 +184,26 @@ } } +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -143,4 +279,17 @@ writer.WriteNumberValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "integer", Format = "int64"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt new file mode 100644 index 000000000..e82a6bc07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Core3_1.verified.txt @@ -0,0 +1,297 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt new file mode 100644 index 000000000..e82a6bc07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet6_0.verified.txt @@ -0,0 +1,297 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt new file mode 100644 index 000000000..e82a6bc07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.DotNet7_0.verified.txt @@ -0,0 +1,297 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt new file mode 100644 index 000000000..e82a6bc07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.Net4_8.verified.txt @@ -0,0 +1,297 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public MassTransit.NewId Value { get; } + + public MyTestId(MassTransit.NewId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); + public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToGuid(), + value => new MyTestId(MassTransit.NewId.FromGuid(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value.ToGuid(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || + sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MassTransit.NewId newIdValue => new MyTestId(newIdValue), + System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MassTransit.NewId)) + { + return idValue.Value; + } + + if (destinationType == typeof(System.Guid)) + { + return idValue.Value.ToGuid(); + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToGuid().ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToGuid()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToGuid()); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt index c2c044246..e82a6bc07 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=MassTransitNewId.verified.txt @@ -12,7 +12,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public MassTransit.NewId Value { get; } @@ -34,10 +35,121 @@ public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } + + +//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 { public EfCoreValueConverter() : this(null) { } @@ -67,6 +179,27 @@ } } + +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -148,4 +281,17 @@ writer.WriteStringValue(value.Value.ToGuid()); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt new file mode 100644 index 000000000..604eb7a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Core3_1.verified.txt @@ -0,0 +1,320 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt new file mode 100644 index 000000000..604eb7a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet6_0.verified.txt @@ -0,0 +1,320 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt new file mode 100644 index 000000000..604eb7a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.DotNet7_0.verified.txt @@ -0,0 +1,320 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt new file mode 100644 index 000000000..604eb7a60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.Net4_8.verified.txt @@ -0,0 +1,320 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string? Value { get; } + + public MyTestId(string? value) + { + Value = value; + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value?.GetHashCode() ?? 0; + public override string? ToString() => Value; + + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + return new MyTestId(s); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + null => new MyTestId(null), + System.DBNull => new MyTestId(null), + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string? Convert(MyTestId source, string? destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value) + { + if (value is null) + { + return new MyTestId(null); + } + + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is null) + { + serializer.Serialize(writer, null); + } + else + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value is null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value); + } + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt index 975c29ade..604eb7a60 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=NullableString.verified.txt @@ -13,7 +13,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public string? Value { get; } @@ -42,6 +43,7 @@ public override int GetHashCode() => Value?.GetHashCode() ?? 0; public override string? ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); public int CompareTo(MyTestId other) @@ -55,6 +57,114 @@ }; } + 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 { public EfCoreValueConverter() : this(null) { } @@ -71,6 +181,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 +196,26 @@ } } +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType) @@ -172,4 +303,18 @@ } } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = true; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt new file mode 100644 index 000000000..a8b8043f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt @@ -0,0 +1,305 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt new file mode 100644 index 000000000..a8b8043f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt @@ -0,0 +1,305 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt new file mode 100644 index 000000000..a8b8043f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt @@ -0,0 +1,305 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt new file mode 100644 index 000000000..a8b8043f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt @@ -0,0 +1,305 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId + { + public string Value { get; } + + public MyTestId(string value) + { + Value = value ?? throw new System.ArgumentNullException(nameof(value)); + } + + public static readonly MyTestId Empty = new MyTestId(string.Empty); + + public bool Equals(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => true, + (null, _) => false, + (_, null) => false, + (_, _) => Value.Equals(other.Value), + }; + } + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value; + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) + { + return (Value, other.Value) switch + { + (null, null) => 0, + (null, _) => -1, + (_, null) => 1, + (_, _) => Value.CompareTo(other.Value), + }; + } + + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.AnsiString; + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue => new MyTestId(stringValue), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + + +#if !FAKE_CODE + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + public string Convert(MyTestId source, string destination, AutoMapper.ResolutionContext context) + { + return source.Value; + } + } +#endif + + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + var stringValue = value as string; + if (stringValue is not null) + { + return new MyTestId(stringValue); + } + + return base.ConvertFrom(context, culture, value); + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(string)) + { + return idValue.Value; + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + if (objectType == typeof(MyTestId?)) + { + var value = serializer.Deserialize(reader); + + return value is null ? null : new MyTestId(value); + } + + return new MyTestId(serializer.Deserialize(reader)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(reader.GetString()); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt index 3e93ee6b2..a8b8043f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt @@ -12,7 +12,8 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] - readonly partial struct MyTestId : System.IComparable, System.IEquatable + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable, System.IParsable, System.IConvertible, IStronglyTypedId { public string Value { get; } @@ -55,6 +56,114 @@ }; } + public static MyTestId Parse(string s, IFormatProvider? provider) + { + throw new NotImplementedException(); + } + + public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result) + { + result = new MyTestId(s); + return true; + } + + +//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 { public EfCoreValueConverter() : this(null) { } @@ -71,6 +180,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 +193,26 @@ } } +#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 + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) @@ -158,4 +288,18 @@ writer.WriteStringValue(value.Value); } } + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = ""}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + schema.Nullable = false; + } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..03bb3dd86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.Core3_1.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + 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=AutoMapper_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..03bb3dd86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.DotNet6_0.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + 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=AutoMapper_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..03bb3dd86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.DotNet7_0.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + 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=AutoMapper_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..03bb3dd86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.Net4_8.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + 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=AutoMapper_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.verified.txt new file mode 100644 index 000000000..03bb3dd86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=AutoMapper_i=None.verified.txt @@ -0,0 +1,46 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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 + public class AutoMapperTypeConverter : AutoMapper.ITypeConverter + { + 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=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt similarity index 71% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt index 23efe4b09..d00e6e636 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Core3_1.verified.txt @@ -11,15 +11,15 @@ readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static 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,7 +27,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(); @@ -38,17 +37,27 @@ { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { - parameter.Value = value.Value.ToGuid(); + parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) { return value switch { - System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), - string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet6_0.verified.txt similarity index 70% 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=DapperTypeHandler_i=None.DotNet6_0.verified.txt index f8d24bbf5..d00e6e636 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=DapperTypeHandler_i=None.DotNet6_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,7 +27,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(); @@ -38,17 +38,26 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) { return value switch { - int intValue => new MyTestId(intValue), - long longValue when longValue < int.MaxValue => new MyTestId((int)longValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt similarity index 67% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt index 35fd482b5..d00e6e636 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.DotNet7_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,7 +27,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(); @@ -38,18 +38,26 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) { return value switch { - long longValue => new MyTestId(longValue), - int intValue => new MyTestId(intValue), - short shortValue => new MyTestId(shortValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt similarity index 61% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt index 749dd1dbd..d00e6e636 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=DapperTypeHandler_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.Net4_8.verified.txt @@ -11,34 +11,25 @@ readonly partial struct MyTestId { - public string Value { get; } + public System.Guid Value { get; } - public MyTestId(string value) + public MyTestId(System.Guid value) { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + Value = value; } - public static readonly MyTestId Empty = new MyTestId(string.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); - public override string ToString() => Value; + public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); @@ -47,15 +38,26 @@ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) { parameter.Value = value.Value; + parameter.DbType = System.Data.DbType.Guid; } public override MyTestId Parse(object value) { return value switch { - string stringValue => new MyTestId(stringValue), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } } + +#pragma warning disable CA2255 + [System.Runtime.CompilerServices.ModuleInitializerAttribute] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new DapperTypeHandler()); + } +#pragma warning restore CA2255 + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=DapperTypeHandler_i=None.verified.txt index bb6b344ab..d00e6e636 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 @@ -27,7 +27,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 +38,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 +51,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=Int_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.verified.txt similarity index 83% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.verified.txt index d3e50467b..9609e0712 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Core3_1.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,14 +27,13 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) 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=EfCoreValueConverter_i=None.DotNet6_0.verified.txt similarity index 83% 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=EfCoreValueConverter_i=None.DotNet6_0.verified.txt index cb5754863..9609e0712 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=EfCoreValueConverter_i=None.DotNet6_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,14 +27,13 @@ if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) 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=Guid_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt similarity index 80% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt index 9114f2d54..9609e0712 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.DotNet7_0.verified.txt @@ -11,15 +11,15 @@ readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static 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,7 +27,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,8 +38,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value.ToGuid(), - value => new MyTestId(MassTransit.NewId.FromGuid(value)), + id => id.Value, + value => new MyTestId(value), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt similarity index 68% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt index 05700f78d..9609e0712 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=EfCoreValueConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=EfCoreValueConverter_i=None.Net4_8.verified.txt @@ -11,38 +11,29 @@ readonly partial struct MyTestId { - public string Value { get; } + public System.Guid Value { get; } - public MyTestId(string value) + public MyTestId(System.Guid value) { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + Value = value; } - public static readonly MyTestId Empty = new MyTestId(string.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); - public override string ToString() => Value; + public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) 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..9609e0712 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 @@ -27,7 +27,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=Long_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.verified.txt similarity index 83% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.verified.txt index 7052a3b27..c78da82e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Core3_1.verified.txt @@ -12,14 +12,15 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] readonly partial struct MyTestId { - 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) @@ -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(); @@ -49,8 +49,8 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { - var result = serializer.Deserialize(reader); - return result.HasValue ? new MyTestId(result.Value) : null; + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt similarity index 83% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt index b4d9738e7..c78da82e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet6_0.verified.txt @@ -12,14 +12,15 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] readonly partial struct MyTestId { - 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,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(); @@ -49,8 +49,8 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { - var result = serializer.Deserialize(reader); - return result.HasValue ? new MyTestId(result.Value) : null; + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt similarity index 81% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt index 181d85e44..c78da82e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.DotNet7_0.verified.txt @@ -12,15 +12,15 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -28,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(); @@ -45,13 +44,13 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { var id = (MyTestId)value; - serializer.Serialize(writer, id.Value.ToGuid()); + serializer.Serialize(writer, id.Value); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var guid = serializer.Deserialize(reader); - return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null; + return guid.HasValue ? new MyTestId(guid.Value) : null; } } } 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=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt similarity index 66% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt index d7596bf2b..c78da82e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=NewtonsoftJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.Net4_8.verified.txt @@ -12,34 +12,25 @@ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] readonly partial struct MyTestId { - public string Value { get; } + public System.Guid Value { get; } - public MyTestId(string value) + public MyTestId(System.Guid value) { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + Value = value; } - public static readonly MyTestId Empty = new MyTestId(string.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); - public override string ToString() => Value; + public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); @@ -58,14 +49,8 @@ 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)); + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyTestId(guid.Value) : null; } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=NewtonsoftJson_i=None.verified.txt index db59f66c1..c78da82e0 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 @@ -28,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=Long_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Core3_1.verified.txt similarity index 83% 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=IComparable.Core3_1.verified.txt index c38fa63f6..1a12aac12 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=IComparable.Core3_1.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId : System.IComparable { - 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,7 +27,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=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt similarity index 83% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt index d76d3afd2..1a12aac12 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet6_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId : System.IComparable { - 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,7 +27,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=MassTransitNewId_c=None_i=IComparable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt similarity index 82% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt index 3a19fc65e..1a12aac12 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IComparable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.DotNet7_0.verified.txt @@ -11,15 +11,15 @@ readonly partial struct MyTestId : System.IComparable { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static 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,7 +27,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.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Net4_8.verified.txt new file mode 100644 index 000000000..1a12aac12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IComparable.Net4_8.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } 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..1a12aac12 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 @@ -27,7 +27,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=IConvertible.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.Core3_1.verified.txt new file mode 100644 index 000000000..380c40d64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.Core3_1.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IConvertible + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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=IConvertible.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.DotNet6_0.verified.txt new file mode 100644 index 000000000..380c40d64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.DotNet6_0.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IConvertible + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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=IConvertible.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.DotNet7_0.verified.txt new file mode 100644 index 000000000..380c40d64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.DotNet7_0.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IConvertible + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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=IConvertible.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.Net4_8.verified.txt new file mode 100644 index 000000000..380c40d64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.Net4_8.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IConvertible + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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=IConvertible.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.verified.txt new file mode 100644 index 000000000..380c40d64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IConvertible.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IConvertible + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string 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=Long_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.verified.txt similarity index 82% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.verified.txt index 13b9b1ac7..88eaec1e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Core3_1.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId : System.IEquatable { - 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,7 +27,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=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet6_0.verified.txt similarity index 82% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet6_0.verified.txt index 5d23b64a1..88eaec1e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet6_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId : System.IEquatable { - 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,7 +27,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=MassTransitNewId_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet7_0.verified.txt similarity index 81% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet7_0.verified.txt index b4c2d5867..88eaec1e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=IEquatable.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.DotNet7_0.verified.txt @@ -11,15 +11,15 @@ readonly partial struct MyTestId : System.IEquatable { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static 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,7 +27,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=IEquatable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Net4_8.verified.txt new file mode 100644 index 000000000..88eaec1e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.Net4_8.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IEquatable.verified.txt index a13e9bec6..88eaec1e0 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 @@ -27,7 +27,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=IParsable.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Core3_1.verified.txt new file mode 100644 index 000000000..e1c6cb649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Core3_1.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt new file mode 100644 index 000000000..e1c6cb649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet6_0.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt new file mode 100644 index 000000000..e1c6cb649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.DotNet7_0.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt new file mode 100644 index 000000000..e1c6cb649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.Net4_8.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt new file mode 100644 index 000000000..e1c6cb649 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IParsable.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IParsable + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + +#nullable enable + public static MyTestId Parse(string s, System.IFormatProvider? provider) + { + return new MyTestId(Guid.Parse(s, provider)); + } + + public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result) + { + long res = 0; + var ok = Guid.TryParse(s, provider, out res); + result = new MyTestId(res); + return ok; + } +#nullable disable + + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.Core3_1.verified.txt new file mode 100644 index 000000000..6850318e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.Core3_1.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + 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=IStronglyTypedId.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.DotNet6_0.verified.txt new file mode 100644 index 000000000..6850318e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.DotNet6_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + 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=IStronglyTypedId.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.DotNet7_0.verified.txt new file mode 100644 index 000000000..6850318e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.DotNet7_0.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + 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=IStronglyTypedId.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.Net4_8.verified.txt new file mode 100644 index 000000000..6850318e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.Net4_8.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + 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=IStronglyTypedId.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.verified.txt new file mode 100644 index 000000000..6850318e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=IStronglyTypedId.verified.txt @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : IStronglyTypedId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public static IStronglyTypedId Create(System.Guid value) + { + return new MyTestId(value); + } + + } 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=None_i=None.Core3_1.verified.txt similarity index 82% 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=None_i=None.Core3_1.verified.txt index 45272d34a..500b02bca 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=None_i=None.Core3_1.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,7 +27,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=Long_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet6_0.verified.txt similarity index 82% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet6_0.verified.txt index af1658c35..500b02bca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet6_0.verified.txt @@ -11,14 +11,15 @@ readonly partial struct MyTestId { - 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,7 +27,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=MassTransitNewId_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet7_0.verified.txt similarity index 80% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet7_0.verified.txt index 7e4892f41..500b02bca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=None_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.DotNet7_0.verified.txt @@ -11,15 +11,15 @@ readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static 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,7 +27,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=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..500b02bca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.Net4_8.verified.txt @@ -0,0 +1,35 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=None_i=None.verified.txt index fad5ea0b5..500b02bca 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 @@ -27,7 +27,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=SwaggerSchemaFilter_i=None.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt new file mode 100644 index 000000000..f8406fb5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Core3_1.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt new file mode 100644 index 000000000..f8406fb5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet6_0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt new file mode 100644 index 000000000..f8406fb5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.DotNet7_0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt new file mode 100644 index 000000000..f8406fb5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.Net4_8.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt new file mode 100644 index 000000000..f8406fb5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SwaggerSchemaFilter_i=None.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Swashbuckle.AspNetCore.Annotations.SwaggerSchemaFilter(typeof(MyTestIdSchemaFilter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter + { + public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context) + { + var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema {Type = "string", Format = "uuid"}; + schema.Type = idSchema.Type; + schema.Format = idSchema.Format; + schema.Example = idSchema.Example; + schema.Default = idSchema.Default; + schema.Properties = idSchema.Properties; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.verified.txt similarity index 82% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.verified.txt index 17a45333f..f1c469105 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Core3_1.verified.txt @@ -12,14 +12,15 @@ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] readonly partial struct MyTestId { - 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) @@ -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(); @@ -38,12 +38,12 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - return new MyTestId(reader.GetInt64()); + return new MyTestId(System.Guid.Parse(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) { - writer.WriteNumberValue(value.Value); + writer.WriteStringValue(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet6_0.verified.txt similarity index 82% 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=SystemTextJson_i=None.DotNet6_0.verified.txt index 7c016c3be..f1c469105 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=SystemTextJson_i=None.DotNet6_0.verified.txt @@ -12,14 +12,15 @@ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] readonly partial struct MyTestId { - 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,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(); @@ -38,12 +38,12 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - return new MyTestId(reader.GetInt32()); + return new MyTestId(System.Guid.Parse(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) { - writer.WriteNumberValue(value.Value); + writer.WriteStringValue(value.Value); } } } 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=Guid_c=SystemTextJson_i=None.DotNet7_0.verified.txt similarity index 80% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet7_0.verified.txt index 3de0600ab..f1c469105 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.DotNet7_0.verified.txt @@ -12,15 +12,15 @@ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -28,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,12 +38,12 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - return new MyTestId(MassTransit.NewId.FromGuid(reader.GetGuid())); + return new MyTestId(System.Guid.Parse(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) { - writer.WriteStringValue(value.Value.ToGuid()); + writer.WriteStringValue(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt similarity index 71% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt index 1396ccae3..f1c469105 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=SystemTextJson_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=SystemTextJson_i=None.Net4_8.verified.txt @@ -12,34 +12,25 @@ [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] readonly partial struct MyTestId { - public string Value { get; } + public System.Guid Value { get; } - public MyTestId(string value) + public MyTestId(System.Guid value) { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + Value = value; } - public static readonly MyTestId Empty = new MyTestId(string.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); - public override string ToString() => Value; + public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); @@ -47,7 +38,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - return new MyTestId(reader.GetString()); + return new MyTestId(System.Guid.Parse(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_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..f1c469105 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 @@ -28,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=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt similarity index 78% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt index 39f1e9331..8191145ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Int_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Core3_1.verified.txt @@ -12,14 +12,15 @@ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] readonly partial struct MyTestId { - 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,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(); @@ -38,29 +38,29 @@ { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { return value switch { - int intValue => new MyTestId(intValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => base.ConvertFrom(context, culture, value), }; } public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); } public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { if (value is MyTestId idValue) { - if (destinationType == typeof(int)) + if (destinationType == typeof(System.Guid)) { return idValue.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=Guid_c=TypeConverter_i=None.DotNet6_0.verified.txt similarity index 75% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet6_0.verified.txt index e22d0478c..8191145ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Long_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet6_0.verified.txt @@ -12,14 +12,15 @@ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] readonly partial struct MyTestId { - 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) @@ -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(); @@ -38,31 +38,29 @@ { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { return value switch { - long longValue => new MyTestId(longValue), - int intValue => new MyTestId(intValue), - short shortValue => new MyTestId(shortValue), - string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => base.ConvertFrom(context, culture, value), }; } public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); } public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { if (value is MyTestId idValue) { - if (destinationType == typeof(long)) + if (destinationType == typeof(System.Guid)) { return idValue.Value; } 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=Guid_c=TypeConverter_i=None.DotNet7_0.verified.txt similarity index 72% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet7_0.verified.txt index 757eb4ebb..8191145ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=MassTransitNewId_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.DotNet7_0.verified.txt @@ -12,15 +12,15 @@ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] readonly partial struct MyTestId { - public MassTransit.NewId Value { get; } + public System.Guid Value { get; } - public MyTestId(MassTransit.NewId value) + public MyTestId(System.Guid value) { Value = value; } - public static MyTestId New() => new MyTestId(MassTransit.NewId.Next()); - public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) @@ -28,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,44 +38,36 @@ { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || - sourceType == typeof(string) || base.CanConvertFrom - (context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { return value switch { - MassTransit.NewId newIdValue => new MyTestId(newIdValue), - System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)), - string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)), + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), _ => base.ConvertFrom(context, culture, value), }; } public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); } public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { if (value is MyTestId idValue) { - if (destinationType == typeof(MassTransit.NewId)) + if (destinationType == typeof(System.Guid)) { return idValue.Value; } - if (destinationType == typeof(System.Guid)) - { - return idValue.Value.ToGuid(); - } - if (destinationType == typeof(string)) { - return idValue.Value.ToGuid().ToString(); + return idValue.Value.ToString(); } } 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=Guid_c=TypeConverter_i=None.Net4_8.verified.txt similarity index 64% rename from test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt rename to test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Net4_8.verified.txt index d35ac20a3..8191145ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=String_c=TypeConverter_i=None.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.Net4_8.verified.txt @@ -12,34 +12,25 @@ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] readonly partial struct MyTestId { - public string Value { get; } + public System.Guid Value { get; } - public MyTestId(string value) + public MyTestId(System.Guid value) { - Value = value ?? throw new System.ArgumentNullException(nameof(value)); + Value = value; } - public static readonly MyTestId Empty = new MyTestId(string.Empty); + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); - public bool Equals(MyTestId other) - { - return (Value, other.Value) switch - { - (null, null) => true, - (null, _) => false, - (_, null) => false, - (_, _) => Value.Equals(other.Value), - }; - } + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj is MyTestId other && Equals(other); } - public override int GetHashCode() => Value.GetHashCode(); - public override string ToString() => Value; + public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); @@ -47,33 +38,37 @@ { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { - var stringValue = value as string; - if (stringValue is not null) + return value switch { - return new MyTestId(stringValue); - } - - return base.ConvertFrom(context, culture, value); + System.Guid guidValue => new MyTestId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result), + _ => base.ConvertFrom(context, culture, value), + }; } public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) { - return sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); } public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { if (value is MyTestId idValue) { - if (destinationType == typeof(string)) + if (destinationType == typeof(System.Guid)) { return idValue.Value; } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } } return base.ConvertTo(context, culture, value, destinationType); diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_type=Guid_c=TypeConverter_i=None.verified.txt index 295e92baf..8191145ea 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 @@ -28,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=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=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.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.verified.txt new file mode 100644 index 000000000..aa83dffa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Core3_1.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_0.verified.txt new file mode 100644 index 000000000..aa83dffa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet6_0.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_0.verified.txt new file mode 100644 index 000000000..aa83dffa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.DotNet7_0.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt new file mode 100644 index 000000000..aa83dffa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.Net4_8.verified.txt @@ -0,0 +1,55 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt index ff61cdde7..aa83dffa7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=0.verified.txt @@ -32,7 +32,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.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Core3_1.verified.txt new file mode 100644 index 000000000..02e2c1cb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Core3_1.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_0.verified.txt new file mode 100644 index 000000000..02e2c1cb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet6_0.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_0.verified.txt new file mode 100644 index 000000000..02e2c1cb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.DotNet7_0.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt new file mode 100644 index 000000000..02e2c1cb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.Net4_8.verified.txt @@ -0,0 +1,58 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt index dacf3158c..02e2c1cb0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=1.verified.txt @@ -34,7 +34,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.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Core3_1.verified.txt new file mode 100644 index 000000000..a3139783d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Core3_1.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_0.verified.txt new file mode 100644 index 000000000..a3139783d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet6_0.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_0.verified.txt new file mode 100644 index 000000000..a3139783d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.DotNet7_0.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt new file mode 100644 index 000000000..a3139783d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.Net4_8.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTestNamespace +{ + partial class OuterLayer1 + { + partial class OuterLayer0 + { + partial record InnerMost + { + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public System.Guid Value { get; } + + public MyTestId(System.Guid value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(System.Guid.NewGuid()); + public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyTestId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt index b71458e7e..a3139783d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdWithNestedClassCorrectly_nestedClassCount=2.verified.txt @@ -36,7 +36,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.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..815503a9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Core3_1.verified.txt @@ -0,0 +1,348 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..815503a9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet6_0.verified.txt @@ -0,0 +1,348 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..815503a9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.DotNet7_0.verified.txt @@ -0,0 +1,348 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..815503a9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.Net4_8.verified.txt @@ -0,0 +1,348 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt index e397007be..815503a9a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateDefaultIdInGlobalNamespace.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -247,7 +278,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Core3_1.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet6_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.DotNet7_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.Net4_8.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt index 0b021b89a..bfdb5963e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInFileScopedNamespace.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Core3_1.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet6_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.DotNet7_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..bfdb5963e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.Net4_8.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt index 0b021b89a..bfdb5963e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdInNamespace.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt index 1be7dd3cb..6c9814159 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt index a7e3384bc..a2de00be4 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=SystemTextJson.verified.txt index e8ca99b42..7f8f5c0d7 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=TypeConverter.verified.txt index 62ccb4765..35821cce9 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Core3_1.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.Net4_8.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithNamedParameters_backingType=Guid_converter=null.verified.txt index af074ad91..90d789929 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..6c9814159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,323 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson, SystemTextJson.verified.txt index 1be7dd3cb..6c9814159 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Core3_1.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet6_0.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.DotNet7_0.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt new file mode 100644 index 000000000..a2de00be4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.Net4_8.verified.txt @@ -0,0 +1,309 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=NewtonsoftJson.verified.txt index a7e3384bc..a2de00be4 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Core3_1.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet6_0.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.DotNet7_0.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt new file mode 100644 index 000000000..7f8f5c0d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.Net4_8.verified.txt @@ -0,0 +1,302 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new MyId(System.Guid.Parse(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyId value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=SystemTextJson.verified.txt index e8ca99b42..7f8f5c0d7 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Core3_1.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet6_0.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.DotNet7_0.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt new file mode 100644 index 000000000..35821cce9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.Net4_8.verified.txt @@ -0,0 +1,330 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=TypeConverter.verified.txt index 62ccb4765..35821cce9 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,7 +279,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Core3_1.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet6_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.DotNet7_0.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt new file mode 100644 index 000000000..90d789929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.Net4_8.verified.txt @@ -0,0 +1,351 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyTests.TestNameSpace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateIdWithPositionalParameters_backingType=Guid_converter=null.verified.txt index af074ad91..90d789929 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,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt new file mode 100644 index 000000000..3e27ed294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Core3_1.verified.txt @@ -0,0 +1,454 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt new file mode 100644 index 000000000..3e27ed294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet6_0.verified.txt @@ -0,0 +1,454 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt new file mode 100644 index 000000000..3e27ed294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.DotNet7_0.verified.txt @@ -0,0 +1,454 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt new file mode 100644 index 000000000..3e27ed294 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.Net4_8.verified.txt @@ -0,0 +1,454 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V1 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} + +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace MyContracts.V2 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt index 7e73d73fe..3e27ed294 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateMultipleIdsWithSameName.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -249,7 +280,6 @@ 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(); @@ -353,7 +383,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..4f5c8cdbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Core3_1.verified.txt @@ -0,0 +1,354 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..4f5c8cdbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet6_0.verified.txt @@ -0,0 +1,354 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..4f5c8cdbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.DotNet7_0.verified.txt @@ -0,0 +1,354 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..4f5c8cdbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.Net4_8.verified.txt @@ -0,0 +1,354 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt index c259d55ac..4f5c8cdbf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateNestedIdInFileScopeNamespace.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -251,7 +282,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt new file mode 100644 index 000000000..1720d4469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Core3_1.verified.txt @@ -0,0 +1,360 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt new file mode 100644 index 000000000..1720d4469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet6_0.verified.txt @@ -0,0 +1,360 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt new file mode 100644 index 000000000..1720d4469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.DotNet7_0.verified.txt @@ -0,0 +1,360 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt new file mode 100644 index 000000000..1720d4469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.Net4_8.verified.txt @@ -0,0 +1,360 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace SomeNamespace +{ + partial class ParentClass where T: new() + { + partial record InnerClass + { + partial struct InnerStruct + { + [Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))] + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public System.Guid Value { get; } + + public MyId(System.Guid value) + { + Value = value; + } + + public static MyId New() => new MyId(System.Guid.NewGuid()); + public static readonly MyId Empty = new MyId(System.Guid.Empty); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + + class MyIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + System.Guid guidValue => new MyId(guidValue), + string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyId(result), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyId idValue) + { + if (destinationType == typeof(System.Guid)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var guid = serializer.Deserialize(reader); + return guid.HasValue ? new MyId(guid.Value) : null; + } + } + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt index ebcdb6716..1720d4469 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanGenerateVeryNestedIdInFileScopeNamespace.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -255,7 +286,6 @@ 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(); diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt new file mode 100644 index 000000000..2bb973bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Core3_1.verified.txt @@ -0,0 +1,286 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt new file mode 100644 index 000000000..2bb973bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet6_0.verified.txt @@ -0,0 +1,286 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt new file mode 100644 index 000000000..2bb973bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.DotNet7_0.verified.txt @@ -0,0 +1,286 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt new file mode 100644 index 000000000..2bb973bdd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.Net4_8.verified.txt @@ -0,0 +1,286 @@ +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdAttribute : Attribute + { + /// + /// Make the struct a strongly typed ID + /// + /// The to use to store the strongly-typed ID value. + /// If not set, uses , which defaults to + /// Converters to create for serializing/deserializing the strongly-typed ID value. + /// If not set, uses , which defaults to + /// and + /// Interfaces and patterns the strongly typed id should implement + /// If not set, uses , which defaults to + /// and + public StronglyTypedIdAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The to use to store the strongly-typed ID value + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// JSON library used to serialize/deserialize strongly-typed ID value + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Used to control the default Place on partial structs to make the type a strongly-typed ID + /// + [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] + [System.Diagnostics.Conditional("STRONGLY_TYPED_ID_USAGES")] + internal sealed class StronglyTypedIdDefaultsAttribute : Attribute + { + /// + /// Set the default values used for strongly typed ids + /// + /// The to use to store the strongly-typed ID value. + /// Defaults to + /// JSON library used to serialize/deserialize strongly-typed ID value. + /// Defaults to and + /// Interfaces and patterns the strongly typed id should implement + /// Defaults to and + public StronglyTypedIdDefaultsAttribute( + StronglyTypedIdBackingType backingType = StronglyTypedIdBackingType.Default, + StronglyTypedIdConverter converters = StronglyTypedIdConverter.Default, + StronglyTypedIdImplementations implementations = StronglyTypedIdImplementations.Default) + { + BackingType = backingType; + Converters = converters; + Implementations = implementations; + } + + /// + /// The default to use to store the strongly-typed ID values. + /// + public StronglyTypedIdBackingType BackingType { get; } + + /// + /// The default converters to create for serializing/deserializing strongly-typed ID values. + /// + public StronglyTypedIdConverter Converters { get; } + + /// + /// Interfaces and patterns the strongly typed id should implement + /// + public StronglyTypedIdImplementations Implementations { get; } + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// The to use to store the value of a strongly-typed ID + /// + internal enum StronglyTypedIdBackingType + { + /// + /// Use the default backing type (either the globally configured default, or Guid) + /// + Default = 0, + + Guid = 1, + Int = 2, + String = 3, + Long = 4, + NullableString = 5, + MassTransitNewId = 6, + } +} +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Converters used to to serialize/deserialize strongly-typed ID values + /// + [Flags] + internal enum StronglyTypedIdConverter + { + // Used with HasFlag, so needs to be 1, 2, 4 etc + + /// + /// Don't create any converters for the strongly typed ID + /// + None = 0, + + /// + /// Use the default converters for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + /// + /// Creates a for converting from the strongly typed ID to and from a string + /// + TypeConverter = 2, + + /// + /// Creates a Newtonsoft.Json.JsonConverter for serializing the strongly typed id to its primitive value + /// + NewtonsoftJson = 4, + + /// + /// Creates a System.Text.Json.Serialization.JsonConverter for serializing the strongly typed id to its primitive value + /// + SystemTextJson = 8, + + /// + /// Creates an EF Core Value Converter for extracting the primitive value + /// + EfCoreValueConverter = 16, + + /// + /// Creates a Dapper TypeHandler for converting to and from the type + /// + DapperTypeHandler = 32, + + /// + /// Creates a Swagger SchemaFilter for OpenApi documentation + /// + SwaggerSchemaFilter = 64, + + /// + /// Creates a AutoMapper bidirectional converters + /// + AutoMapper = 128, + + } +} + +#endif +#if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES + +using System; + +namespace StronglyTypedIds +{ + /// + /// Interfaces and patterns the strongly typed id should implement + /// + [Flags] + internal enum StronglyTypedIdImplementations + { + // Used with HasFlag, so needs to be 1, 2, 4, 8 etc + + /// + /// Don't implement any additional members for the strongly typed ID + /// + None = 0, + + /// + /// Use the default implementations for the strongly typed Id. + /// This will be the value provided in the , which falls back to + /// and + /// + Default = 1, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IEquatable = 2, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IComparable = 4, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IParsable = 8, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IConvertible = 16, + + // ReSharper disable once InconsistentNaming + /// + /// Implement the interface + /// + IStronglyTypedId = 32 + } +} + +#endif +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyId : System.IComparable, System.IEquatable + { + public int Value { get; } + + public MyId(int value) + { + Value = value; + } + + public static readonly MyId Empty = new MyId(0); + + public bool Equals(MyId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + + public static bool operator ==(MyId a, MyId b) => a.Equals(b); + public static bool operator !=(MyId a, MyId b) => !(a == b); + public int CompareTo(MyId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt index 02e7314d4..2bb973bdd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/StronglyTypedIdGeneratorTests.CanOverrideDefaultsUsingGlobalAttribute.verified.txt @@ -173,8 +173,20 @@ 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, + } } + #endif #if STRONGLY_TYPED_ID_EMBED_ATTRIBUTES @@ -188,7 +200,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 +225,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 //------------------------------------------------------------------------------ // @@ -248,6 +279,7 @@ 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); diff --git a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs index 383b9a129..db4ec5457 100644 --- a/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs +++ b/test/StronglyTypedIds.Tests/SourceGenerationHelperSnapshotTests.cs @@ -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..0d84ee568 100644 --- a/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj +++ b/test/StronglyTypedIds.Tests/StronglyTypedIds.Tests.csproj @@ -11,6 +11,7 @@ + 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/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..acbc61d68 100644 --- a/version.props +++ b/version.props @@ -1,7 +1,7 @@ - 1.0.0 - beta06 + 1.0.90 + $(VersionPrefix) $(VersionPrefix)-$(VersionSuffix)