diff --git a/src/StructId.Analyzer/ConstructorGenerator.cs b/src/StructId.Analyzer/ConstructorGenerator.cs index 3473eb9..5a4bc4c 100644 --- a/src/StructId.Analyzer/ConstructorGenerator.cs +++ b/src/StructId.Analyzer/ConstructorGenerator.cs @@ -11,6 +11,8 @@ public class ConstructorGenerator() : BaseGenerator( ThisAssembly.Resources.Templates.ConstructorT.Text, ReferenceCheck.TypeExists) { + // NOTE: since we only emit the ctor if the struct doesn't already have one, + // we cannot switch this to the simpler compiled templates, which don't have conditional logic protected override IncrementalValuesProvider OnInitialize(IncrementalGeneratorInitializationContext context, IncrementalValuesProvider source) => base.OnInitialize(context, source.Where(x => x.StructId.DeclaringSyntaxReferences.Select(r => r.GetSyntax()).OfType().All(s => s.ParameterList == null))); diff --git a/src/StructId.Analyzer/ConversionGenerator.cs b/src/StructId.Analyzer/ConversionGenerator.cs deleted file mode 100644 index 64c71f7..0000000 --- a/src/StructId.Analyzer/ConversionGenerator.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace StructId; - -[Generator(LanguageNames.CSharp)] -public class ConversionGenerator() : BaseGenerator( - "System.Object", - ThisAssembly.Resources.Templates.Conversion.Text, - ThisAssembly.Resources.Templates.ConversionT.Text, - ReferenceCheck.TypeExists); \ No newline at end of file diff --git a/src/StructId.Analyzer/TemplatedGenerator.cs b/src/StructId.Analyzer/TemplatedGenerator.cs index dd7b141..a3585d2 100644 --- a/src/StructId.Analyzer/TemplatedGenerator.cs +++ b/src/StructId.Analyzer/TemplatedGenerator.cs @@ -187,7 +187,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var type = idType.DeclaringSyntaxReferences[0].GetSyntax(cancellation) as TypeDeclarationSyntax; var iface = type?.BaseList?.Types.FirstOrDefault()?.Type; if (type == null || iface == null) - return new Template(structId, idType, attribute, known); + return new Template(structId, idType, attribute, known) { OriginalTId = idType }; if (x.Right.Compilation.GetSemanticModel(type.SyntaxTree).GetSymbolInfo(iface).Symbol is not INamedTypeSymbol ifaceType) return new Template(structId, idType, attribute, known); diff --git a/src/StructId/ResourceTemplates/Conversion.cs b/src/StructId/ResourceTemplates/Conversion.cs deleted file mode 100644 index 948934b..0000000 --- a/src/StructId/ResourceTemplates/Conversion.cs +++ /dev/null @@ -1,7 +0,0 @@ -// - -readonly partial record struct Self -{ - public static implicit operator string(Self id) => id.Value; - public static explicit operator Self(string value) => new(value); -} \ No newline at end of file diff --git a/src/StructId/Templates/Conversion.cs b/src/StructId/Templates/Conversion.cs new file mode 100644 index 0000000..8c49b16 --- /dev/null +++ b/src/StructId/Templates/Conversion.cs @@ -0,0 +1,10 @@ +// + +using StructId; + +[TStructId] +file partial record struct TSelf(string Value) +{ + public static implicit operator string(TSelf id) => id.Value; + public static explicit operator TSelf(string value) => new(value); +} \ No newline at end of file diff --git a/src/StructId/ResourceTemplates/ConversionT.cs b/src/StructId/Templates/ConversionT.cs similarity index 61% rename from src/StructId/ResourceTemplates/ConversionT.cs rename to src/StructId/Templates/ConversionT.cs index 113f851..dbab50e 100644 --- a/src/StructId/ResourceTemplates/ConversionT.cs +++ b/src/StructId/Templates/ConversionT.cs @@ -1,7 +1,12 @@ // -readonly partial record struct TSelf +using StructId; + +[TStructId] +file partial record struct TSelf(TId Value) { public static implicit operator TId(TSelf id) => id.Value; public static explicit operator TSelf(TId value) => new(value); -} \ No newline at end of file +} + +file record struct TId; \ No newline at end of file