Skip to content

Commit

Permalink
Added support for internal identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Nov 16, 2024
1 parent fd8389d commit 772ec6b
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;

Expand Down Expand Up @@ -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}}>,
"""
);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -730,13 +732,15 @@ private readonly record struct Identifier(
EquatableImmutableArray<Declaration> ContainingDeclarations,
string Name,
bool IsStruct,
bool IsPublic,
string DeclaredValueType,
string FullValueType
)
{
public EquatableImmutableArray<Declaration> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<!-- Output -->
<PropertyGroup>
<AssemblyName>LightResults.Extensions.GeneratedIdentifier</AssemblyName>
<Version>9.0.0-preview.8</Version>
<Version>9.0.0-preview.9</Version>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<FileVersion>9.0.0.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace MyProject.Identifiers;

[TypeConverter(typeof(TestGuidIdTypeConverter))]
[JsonConverter(typeof(TestGuidIdJsonConverter))]
readonly partial struct TestGuidId :
public readonly partial struct TestGuidId :
ICreatableValueObject<Guid, TestGuidId>,
IParsableValueObject<TestGuidId>,
IValueObject<Guid, TestGuidId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -215,7 +215,7 @@ public class TestGuidIdTypeConverter : TypeConverter
}
}

public class TestGuidIdJsonConverter : JsonConverter<TestGuidId>
public sealed class TestGuidIdJsonConverter : JsonConverter<TestGuidId>
{
public override void Write(Utf8JsonWriter writer, TestGuidId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects;

[TypeConverter(typeof(TestGuidIdTypeConverter))]
[JsonConverter(typeof(TestGuidIdJsonConverter))]
readonly partial struct TestGuidId :
public readonly partial struct TestGuidId :
ICreatableValueObject<Guid, TestGuidId>,
IParsableValueObject<TestGuidId>,
IValueObject<Guid, TestGuidId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -213,7 +213,7 @@ public class TestGuidIdTypeConverter : TypeConverter
}
}

public class TestGuidIdJsonConverter : JsonConverter<TestGuidId>
public sealed class TestGuidIdJsonConverter : JsonConverter<TestGuidId>
{
public override void Write(Utf8JsonWriter writer, TestGuidId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace MyProject.Identifiers;

[TypeConverter(typeof(TestIntIdTypeConverter))]
[JsonConverter(typeof(TestIntIdJsonConverter))]
readonly partial struct TestIntId :
public readonly partial struct TestIntId :
ICreatableValueObject<int, TestIntId>,
IParsableValueObject<TestIntId>,
IValueObject<int, TestIntId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -218,7 +218,7 @@ public class TestIntIdTypeConverter : TypeConverter
}
}

public class TestIntIdJsonConverter : JsonConverter<TestIntId>
public sealed class TestIntIdJsonConverter : JsonConverter<TestIntId>
{
public override void Write(Utf8JsonWriter writer, TestIntId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects;

[TypeConverter(typeof(TestIntIdTypeConverter))]
[JsonConverter(typeof(TestIntIdJsonConverter))]
readonly partial struct TestIntId :
public readonly partial struct TestIntId :
ICreatableValueObject<int, TestIntId>,
IParsableValueObject<TestIntId>,
IValueObject<int, TestIntId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -216,7 +216,7 @@ public class TestIntIdTypeConverter : TypeConverter
}
}

public class TestIntIdJsonConverter : JsonConverter<TestIntId>
public sealed class TestIntIdJsonConverter : JsonConverter<TestIntId>
{
public override void Write(Utf8JsonWriter writer, TestIntId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace MyProject.Identifiers;

[TypeConverter(typeof(TestLongIdTypeConverter))]
[JsonConverter(typeof(TestLongIdJsonConverter))]
readonly partial struct TestLongId :
public readonly partial struct TestLongId :
ICreatableValueObject<long, TestLongId>,
IParsableValueObject<TestLongId>,
IValueObject<long, TestLongId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -218,7 +218,7 @@ public class TestLongIdTypeConverter : TypeConverter
}
}

public class TestLongIdJsonConverter : JsonConverter<TestLongId>
public sealed class TestLongIdJsonConverter : JsonConverter<TestLongId>
{
public override void Write(Utf8JsonWriter writer, TestLongId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects;

[TypeConverter(typeof(TestLongIdTypeConverter))]
[JsonConverter(typeof(TestLongIdJsonConverter))]
readonly partial struct TestLongId :
public readonly partial struct TestLongId :
ICreatableValueObject<long, TestLongId>,
IParsableValueObject<TestLongId>,
IValueObject<long, TestLongId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -216,7 +216,7 @@ public class TestLongIdTypeConverter : TypeConverter
}
}

public class TestLongIdJsonConverter : JsonConverter<TestLongId>
public sealed class TestLongIdJsonConverter : JsonConverter<TestLongId>
{
public override void Write(Utf8JsonWriter writer, TestLongId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace MyProject.Identifiers;

[TypeConverter(typeof(TestShortIdTypeConverter))]
[JsonConverter(typeof(TestShortIdJsonConverter))]
readonly partial struct TestShortId :
public readonly partial struct TestShortId :
ICreatableValueObject<short, TestShortId>,
IParsableValueObject<TestShortId>,
IValueObject<short, TestShortId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -218,7 +218,7 @@ public class TestShortIdTypeConverter : TypeConverter
}
}

public class TestShortIdJsonConverter : JsonConverter<TestShortId>
public sealed class TestShortIdJsonConverter : JsonConverter<TestShortId>
{
public override void Write(Utf8JsonWriter writer, TestShortId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects;

[TypeConverter(typeof(TestShortIdTypeConverter))]
[JsonConverter(typeof(TestShortIdJsonConverter))]
readonly partial struct TestShortId :
public readonly partial struct TestShortId :
ICreatableValueObject<short, TestShortId>,
IParsableValueObject<TestShortId>,
IValueObject<short, TestShortId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -216,7 +216,7 @@ public class TestShortIdTypeConverter : TypeConverter
}
}

public class TestShortIdJsonConverter : JsonConverter<TestShortId>
public sealed class TestShortIdJsonConverter : JsonConverter<TestShortId>
{
public override void Write(Utf8JsonWriter writer, TestShortId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace MyProject.Identifiers;

[TypeConverter(typeof(TestStringIdTypeConverter))]
[JsonConverter(typeof(TestStringIdJsonConverter))]
sealed partial class TestStringId :
public sealed partial class TestStringId :
ICreatableValueObject<string, TestStringId>,
IValueObject<string, TestStringId>,
IComparable<TestStringId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -198,7 +198,7 @@ public class TestStringIdTypeConverter : TypeConverter
}
}

public class TestStringIdJsonConverter : JsonConverter<TestStringId>
public sealed class TestStringIdJsonConverter : JsonConverter<TestStringId>
{
public override void Write(Utf8JsonWriter writer, TestStringId identifier, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using LightResults.Extensions.ValueObjects;

[TypeConverter(typeof(TestStringIdTypeConverter))]
[JsonConverter(typeof(TestStringIdJsonConverter))]
sealed partial class TestStringId :
public sealed partial class TestStringId :
ICreatableValueObject<string, TestStringId>,
IValueObject<string, TestStringId>,
IComparable<TestStringId>,
Expand Down Expand Up @@ -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)
{
Expand All @@ -196,7 +196,7 @@ public class TestStringIdTypeConverter : TypeConverter
}
}

public class TestStringIdJsonConverter : JsonConverter<TestStringId>
public sealed class TestStringIdJsonConverter : JsonConverter<TestStringId>
{
public override void Write(Utf8JsonWriter writer, TestStringId identifier, JsonSerializerOptions options)
{
Expand Down

0 comments on commit 772ec6b

Please sign in to comment.