Skip to content

Commit

Permalink
Merge pull request #77 from nenoNaninu/use_GetTypesByMetadataName
Browse files Browse the repository at this point in the history
use GetTypesByMetadataName
  • Loading branch information
nenoNaninu authored Feb 7, 2023
2 parents 7a80dd5 + ffc976d commit c449e8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
15 changes: 13 additions & 2 deletions src/Tapper/RoslynExtensions.TypeCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static INamedTypeSymbol[] GetSourceTypes(this Compilation compilation, bo
return TargetTypes;
}

var annotationSymbol = compilation.GetTypeByMetadataName("Tapper.TranspilationSourceAttribute");
var annotationSymbols = compilation.GetTypesByMetadataName("Tapper.TranspilationSourceAttribute");

var namedTypes = includeReferencedAssemblies ? compilation.GetGlobalNamedTypeSymbols() : compilation.GetNamedTypeSymbols();

Expand All @@ -74,7 +74,18 @@ public static INamedTypeSymbol[] GetSourceTypes(this Compilation compilation, bo
return false;
}
return attributes.Any(x => SymbolEqualityComparer.Default.Equals(x.AttributeClass, annotationSymbol));
foreach (var attribute in attributes)
{
foreach (var annotationSymbol in annotationSymbols)
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, annotationSymbol))
{
return true;
}
}
}
return false;
})
.ToArray();

Expand Down
17 changes: 9 additions & 8 deletions src/Tapper/SpecialSymbols.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;

namespace Tapper;

public class SpecialSymbols
{
public INamedTypeSymbol? JsonPropertyNameAttribute { get; }
public INamedTypeSymbol? JsonIgnoreAttribute { get; }
public INamedTypeSymbol? MessagePackKeyAttribute { get; }
public INamedTypeSymbol? MessagePackIgnoreMemberAttribute { get; }
public ImmutableArray<INamedTypeSymbol> JsonPropertyNameAttributes { get; }
public ImmutableArray<INamedTypeSymbol> JsonIgnoreAttributes { get; }
public ImmutableArray<INamedTypeSymbol> MessagePackKeyAttributes { get; }
public ImmutableArray<INamedTypeSymbol> MessagePackIgnoreMemberAttributes { get; }

public SpecialSymbols(Compilation compilation)
{
JsonPropertyNameAttribute = compilation.GetTypeByMetadataName("System.Text.Json.Serialization.JsonPropertyNameAttribute");
JsonIgnoreAttribute = compilation.GetTypeByMetadataName("System.Text.Json.Serialization.JsonIgnoreAttribute");
JsonPropertyNameAttributes = compilation.GetTypesByMetadataName("System.Text.Json.Serialization.JsonPropertyNameAttribute");
JsonIgnoreAttributes = compilation.GetTypesByMetadataName("System.Text.Json.Serialization.JsonIgnoreAttribute");

MessagePackKeyAttribute = compilation.GetTypeByMetadataName("MessagePack.KeyAttribute");
MessagePackIgnoreMemberAttribute = compilation.GetTypeByMetadataName("MessagePack.IgnoreMemberAttribute");
MessagePackKeyAttributes = compilation.GetTypesByMetadataName("MessagePack.KeyAttribute");
MessagePackIgnoreMemberAttributes = compilation.GetTypesByMetadataName("MessagePack.IgnoreMemberAttribute");
}
}
8 changes: 4 additions & 4 deletions src/Tapper/TypeTranslators/DefaultMessageTypeTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ public static (bool IsValid, string Name) GetMemberName(ISymbol memberSymbol, IT
{
foreach (var attr in memberSymbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, options.SpecialSymbols.JsonIgnoreAttribute))
if (options.SpecialSymbols.JsonIgnoreAttributes.Any(x => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, x)))
{
return (false, string.Empty);
}

if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, options.SpecialSymbols.JsonPropertyNameAttribute))
if (options.SpecialSymbols.JsonPropertyNameAttributes.Any(x => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, x)))
{
var name = attr.ConstructorArguments[0].Value!.ToString()!;
return (true, name);
Expand All @@ -143,12 +143,12 @@ public static (bool IsValid, string Name) GetMemberName(ISymbol memberSymbol, IT
{
foreach (var attr in memberSymbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, options.SpecialSymbols.MessagePackIgnoreMemberAttribute))
if (options.SpecialSymbols.MessagePackIgnoreMemberAttributes.Any(x => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, x)))
{
return (false, string.Empty);
}

if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, options.SpecialSymbols.MessagePackKeyAttribute))
if (options.SpecialSymbols.MessagePackKeyAttributes.Any(x => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, x)))
{
if (attr.ConstructorArguments[0].Type?.SpecialType == SpecialType.System_String)
{
Expand Down

0 comments on commit c449e8f

Please sign in to comment.