Skip to content

Commit

Permalink
Remove unused [TStructId<T>] attribute
Browse files Browse the repository at this point in the history
We always infer the TId from the Value primary constructor instead. Simplifies authoring.
  • Loading branch information
kzu committed Dec 7, 2024
1 parent 39bfb79 commit 5268693
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
18 changes: 4 additions & 14 deletions src/StructId.Analyzer/TemplatedGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -41,10 +40,6 @@ record KnownTypes(Compilation Compilation, string StructIdNamespace)
/// StructId.TStructIdAttribute
/// </summary>
public INamedTypeSymbol? TStructId { get; } = Compilation.GetTypeByMetadataName($"{StructIdNamespace}.TStructIdAttribute");
/// <summary>
/// StructId.TStructIdAttribute{T}
/// </summary>
public INamedTypeSymbol? TStructIdT { get; } = Compilation.GetTypeByMetadataName($"{StructIdNamespace}.TStructIdAttribute`1");
}

/// <summary>
Expand Down Expand Up @@ -173,21 +168,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
declaration.ParameterList?.Parameters.Count == 1 &&
declaration.ParameterList.Parameters[0].Identifier.Text == "Value" &&
// And we can locate the TStructIdAttribute type that should be applied to it.
x.Right.TStructId != null && x.Right.TStructIdT != null &&
x.Right.TStructId != null &&
x.Left.GetAttributes().Any(a => a.AttributeClass != null &&
// The attribute should either be the generic or regular TStructIdAttribute
(a.AttributeClass.Is(x.Right.TStructId) || a.AttributeClass.Is(x.Right.TStructIdT))))
(a.AttributeClass.Is(x.Right.TStructId))))
.Select((x, cancellation) =>
{
var (structId, known) = x;
var attribute = structId.GetAttributes().FirstOrDefault(a => a.AttributeClass != null && a.AttributeClass.Is(known.TStructIdT));
if (attribute != null && attribute.AttributeClass!.TypeArguments[0] is INamedTypeSymbol attrType)
return new Template(structId, attrType, attribute, known);

// If we don't have the generic attribute, infer the idType from the required
// primary constructor Value parameter type
// We infer the idType from the required primary constructor Value parameter type
var idType = (INamedTypeSymbol)structId.GetMembers().OfType<IPropertySymbol>().First(p => p.Name == "Value").Type;
attribute = structId.GetAttributes().First(a => a.AttributeClass != null && a.AttributeClass.Is(known.TStructId));
var attribute = structId.GetAttributes().First(a => a.AttributeClass != null && a.AttributeClass.Is(known.TStructId));

// The id type isn't declared in the same file, so we don't do anything fancy with it.
if (idType.DeclaringSyntaxReferences.Length == 0)
Expand Down
13 changes: 1 addition & 12 deletions src/StructId/TStructIdAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
namespace StructId;

/// <summary>
/// Attribute for marking a template type for a struct id based on
/// a generic type parameter, which would implement <see cref="IStructId{TId}"/>.
/// </summary>
/// <typeparam name="TId">Template for the TId to replace in the <see cref="IStructId{TId}"/.></typeparam>
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
public class TStructIdAttribute<TId> : Attribute
{
}

/// <summary>
/// Attribute for marking a template type for a struct id based on a string value,
/// which would implement <see cref="IStructId"/>.
/// Attribute for marking a template type for a struct id.
/// </summary>
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
public class TStructIdAttribute : Attribute
Expand Down

0 comments on commit 5268693

Please sign in to comment.