From 772ec6b4d3d485ba8e1aababa5eae73d7868eaa1 Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Carle <29762210+jscarle@users.noreply.github.com> Date: Sat, 16 Nov 2024 11:46:13 -0500 Subject: [PATCH] Added support for internal identifiers. --- .../GeneratedIdentifierSourceGenerator.cs | 12 ++++++++---- ...ightResults.Extensions.GeneratedIdentifier.csproj | 2 +- ...GenerateGuidIdentifier_WithNamespace.verified.txt | 6 +++--- ...erateGuidIdentifier_WithoutNamespace.verified.txt | 6 +++--- ....GenerateIntIdentifier_WithNamespace.verified.txt | 6 +++--- ...nerateIntIdentifier_WithoutNamespace.verified.txt | 6 +++--- ...GenerateLongIdentifier_WithNamespace.verified.txt | 6 +++--- ...erateLongIdentifier_WithoutNamespace.verified.txt | 6 +++--- ...enerateShortIdentifier_WithNamespace.verified.txt | 6 +++--- ...rateShortIdentifier_WithoutNamespace.verified.txt | 6 +++--- ...nerateStringIdentifier_WithNamespace.verified.txt | 6 +++--- ...ateStringIdentifier_WithoutNamespace.verified.txt | 6 +++--- 12 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs b/src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs index 1137040..fe7f283 100644 --- a/src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs +++ b/src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs @@ -72,6 +72,7 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation var containingDeclarations = namedTypeSymbol.GetContainingDeclarations(cancellationToken); var symbolName = namedTypeSymbol.Name; var isStruct = namedTypeSymbol.TypeKind == TypeKind.Struct; + var isPublic = namedTypeSymbol.DeclaredAccessibility == Accessibility.Public; var attribute = context.Attributes[0].AttributeClass!; if (attribute.TypeArguments.Length != 1) @@ -107,7 +108,7 @@ private static bool Filter(SyntaxNode syntaxNode, CancellationToken cancellation break; } - var symbol = new Identifier(containingDeclarations, symbolName, isStruct, declaredValueType, fullValueType); + var symbol = new Identifier(containingDeclarations, symbolName, isStruct, isPublic, declaredValueType, fullValueType); return symbol; } @@ -119,6 +120,7 @@ private static void GenerateIdentifier(SourceProductionContext context, Immutabl var symbolNamespace = symbol.ContainingDeclarations.ToNamespace(); var symbolName = symbol.Name; var isStruct = symbol.IsStruct; + var isPublic = symbol.IsPublic; var declaredValueType = symbol.DeclaredValueType; var fullValueType = symbol.FullValueType; @@ -157,7 +159,7 @@ namespace {symbolNamespace}; source.AppendLine($$""" [TypeConverter(typeof({{symbolName}}TypeConverter))] [JsonConverter(typeof({{symbolName}}JsonConverter))] - {{(isStruct ? "readonly" : "sealed")}} partial {{(isStruct ? "struct" : "class")}} {{symbolName}} : + {{(isPublic ? "public" : "internal")}} {{(isStruct ? "readonly" : "sealed")}} partial {{(isStruct ? "struct" : "class")}} {{symbolName}} : ICreatableValueObject<{{declaredValueType}}, {{symbolName}}>, """ ); @@ -664,7 +666,7 @@ private static Result Validate({{declaredValueType}} value) ); source.AppendLine($$""" - public class {{symbolName}}TypeConverter : TypeConverter + {{(isPublic ? "public" : "internal")}} sealed class {{symbolName}}TypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -683,7 +685,7 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source """ ); source.AppendLine($$""" - public class {{symbolName}}JsonConverter : JsonConverter<{{symbolName}}> + {{(isPublic ? "public" : "internal")}} sealed class {{symbolName}}JsonConverter : JsonConverter<{{symbolName}}> { public override void Write(Utf8JsonWriter writer, {{symbolName}} identifier, JsonSerializerOptions options) { @@ -730,6 +732,7 @@ private readonly record struct Identifier( EquatableImmutableArray ContainingDeclarations, string Name, bool IsStruct, + bool IsPublic, string DeclaredValueType, string FullValueType ) @@ -737,6 +740,7 @@ string FullValueType public EquatableImmutableArray ContainingDeclarations { get; } = ContainingDeclarations; public string Name { get; } = Name; public bool IsStruct { get; } = IsStruct; + public bool IsPublic { get; } = IsPublic; public string DeclaredValueType { get; } = DeclaredValueType; public string FullValueType { get; } = FullValueType; } diff --git a/src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj b/src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj index 72957b9..c8e1488 100644 --- a/src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj +++ b/src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj @@ -36,7 +36,7 @@ LightResults.Extensions.GeneratedIdentifier - 9.0.0-preview.8 + 9.0.0-preview.9 9.0.0.0 9.0.0.0 en-US diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt index 3633781..2f7616c 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt @@ -21,7 +21,7 @@ namespace MyProject.Identifiers; [TypeConverter(typeof(TestGuidIdTypeConverter))] [JsonConverter(typeof(TestGuidIdJsonConverter))] -readonly partial struct TestGuidId : +public readonly partial struct TestGuidId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -199,7 +199,7 @@ readonly partial struct TestGuidId : } } -public class TestGuidIdTypeConverter : TypeConverter +public sealed class TestGuidIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -215,7 +215,7 @@ public class TestGuidIdTypeConverter : TypeConverter } } -public class TestGuidIdJsonConverter : JsonConverter +public sealed class TestGuidIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestGuidId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt index 119cfd5..976d1bd 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects; [TypeConverter(typeof(TestGuidIdTypeConverter))] [JsonConverter(typeof(TestGuidIdJsonConverter))] -readonly partial struct TestGuidId : +public readonly partial struct TestGuidId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -197,7 +197,7 @@ readonly partial struct TestGuidId : } } -public class TestGuidIdTypeConverter : TypeConverter +public sealed class TestGuidIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -213,7 +213,7 @@ public class TestGuidIdTypeConverter : TypeConverter } } -public class TestGuidIdJsonConverter : JsonConverter +public sealed class TestGuidIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestGuidId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithNamespace.verified.txt index 0407e45..05f99c3 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithNamespace.verified.txt @@ -21,7 +21,7 @@ namespace MyProject.Identifiers; [TypeConverter(typeof(TestIntIdTypeConverter))] [JsonConverter(typeof(TestIntIdJsonConverter))] -readonly partial struct TestIntId : +public readonly partial struct TestIntId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -202,7 +202,7 @@ readonly partial struct TestIntId : } } -public class TestIntIdTypeConverter : TypeConverter +public sealed class TestIntIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -218,7 +218,7 @@ public class TestIntIdTypeConverter : TypeConverter } } -public class TestIntIdJsonConverter : JsonConverter +public sealed class TestIntIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestIntId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithoutNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithoutNamespace.verified.txt index f43c056..5c9022b 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithoutNamespace.verified.txt @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects; [TypeConverter(typeof(TestIntIdTypeConverter))] [JsonConverter(typeof(TestIntIdJsonConverter))] -readonly partial struct TestIntId : +public readonly partial struct TestIntId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -200,7 +200,7 @@ readonly partial struct TestIntId : } } -public class TestIntIdTypeConverter : TypeConverter +public sealed class TestIntIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -216,7 +216,7 @@ public class TestIntIdTypeConverter : TypeConverter } } -public class TestIntIdJsonConverter : JsonConverter +public sealed class TestIntIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestIntId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithNamespace.verified.txt index 3d0df25..0b3e35d 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithNamespace.verified.txt @@ -21,7 +21,7 @@ namespace MyProject.Identifiers; [TypeConverter(typeof(TestLongIdTypeConverter))] [JsonConverter(typeof(TestLongIdJsonConverter))] -readonly partial struct TestLongId : +public readonly partial struct TestLongId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -202,7 +202,7 @@ readonly partial struct TestLongId : } } -public class TestLongIdTypeConverter : TypeConverter +public sealed class TestLongIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -218,7 +218,7 @@ public class TestLongIdTypeConverter : TypeConverter } } -public class TestLongIdJsonConverter : JsonConverter +public sealed class TestLongIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestLongId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithoutNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithoutNamespace.verified.txt index 4f907d2..f466b33 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithoutNamespace.verified.txt @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects; [TypeConverter(typeof(TestLongIdTypeConverter))] [JsonConverter(typeof(TestLongIdJsonConverter))] -readonly partial struct TestLongId : +public readonly partial struct TestLongId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -200,7 +200,7 @@ readonly partial struct TestLongId : } } -public class TestLongIdTypeConverter : TypeConverter +public sealed class TestLongIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -216,7 +216,7 @@ public class TestLongIdTypeConverter : TypeConverter } } -public class TestLongIdJsonConverter : JsonConverter +public sealed class TestLongIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestLongId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithNamespace.verified.txt index e30a340..8d33b39 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithNamespace.verified.txt @@ -21,7 +21,7 @@ namespace MyProject.Identifiers; [TypeConverter(typeof(TestShortIdTypeConverter))] [JsonConverter(typeof(TestShortIdJsonConverter))] -readonly partial struct TestShortId : +public readonly partial struct TestShortId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -202,7 +202,7 @@ readonly partial struct TestShortId : } } -public class TestShortIdTypeConverter : TypeConverter +public sealed class TestShortIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -218,7 +218,7 @@ public class TestShortIdTypeConverter : TypeConverter } } -public class TestShortIdJsonConverter : JsonConverter +public sealed class TestShortIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestShortId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithoutNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithoutNamespace.verified.txt index 5d4e584..d1d8df7 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithoutNamespace.verified.txt @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects; [TypeConverter(typeof(TestShortIdTypeConverter))] [JsonConverter(typeof(TestShortIdJsonConverter))] -readonly partial struct TestShortId : +public readonly partial struct TestShortId : ICreatableValueObject, IParsableValueObject, IValueObject, @@ -200,7 +200,7 @@ readonly partial struct TestShortId : } } -public class TestShortIdTypeConverter : TypeConverter +public sealed class TestShortIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -216,7 +216,7 @@ public class TestShortIdTypeConverter : TypeConverter } } -public class TestShortIdJsonConverter : JsonConverter +public sealed class TestShortIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestShortId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt index 4c53da8..f4e97fa 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt @@ -21,7 +21,7 @@ namespace MyProject.Identifiers; [TypeConverter(typeof(TestStringIdTypeConverter))] [JsonConverter(typeof(TestStringIdJsonConverter))] -sealed partial class TestStringId : +public sealed partial class TestStringId : ICreatableValueObject, IValueObject, IComparable, @@ -182,7 +182,7 @@ sealed partial class TestStringId : } } -public class TestStringIdTypeConverter : TypeConverter +public sealed class TestStringIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -198,7 +198,7 @@ public class TestStringIdTypeConverter : TypeConverter } } -public class TestStringIdJsonConverter : JsonConverter +public sealed class TestStringIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestStringId identifier, JsonSerializerOptions options) { diff --git a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt index 0cdf679..4f9ae97 100644 --- a/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt +++ b/tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects; [TypeConverter(typeof(TestStringIdTypeConverter))] [JsonConverter(typeof(TestStringIdJsonConverter))] -sealed partial class TestStringId : +public sealed partial class TestStringId : ICreatableValueObject, IValueObject, IComparable, @@ -180,7 +180,7 @@ sealed partial class TestStringId : } } -public class TestStringIdTypeConverter : TypeConverter +public sealed class TestStringIdTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) { @@ -196,7 +196,7 @@ public class TestStringIdTypeConverter : TypeConverter } } -public class TestStringIdJsonConverter : JsonConverter +public sealed class TestStringIdJsonConverter : JsonConverter { public override void Write(Utf8JsonWriter writer, TestStringId identifier, JsonSerializerOptions options) {