Skip to content

Commit

Permalink
fix: add GetAttributesCore, modify GetAttributes and HasAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jul 10, 2023
1 parent 5419f34 commit b910c18
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
5 changes: 1 addition & 4 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public IEnumerable<TData> Access<TAttribute, TData>(ISymbol symbol)
{
var attrType = typeof(TAttribute);
var dataType = typeof(TData);
var attrSymbol = _types.Get($"{attrType.Namespace}.{attrType.Name}");

var attrDatas = _symbolAccessor
.GetAttributes(symbol)
.Where(x => SymbolEqualityComparer.Default.Equals(x.AttributeClass?.ConstructedFrom ?? x.AttributeClass, attrSymbol));
var attrDatas = _symbolAccessor.GetAttributes<TAttribute>(symbol);

foreach (var attrData in attrDatas)
{
Expand Down
36 changes: 20 additions & 16 deletions src/Riok.Mapperly/Descriptors/SymbolAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,17 @@ public SymbolAccessor(WellKnownTypes types)
_types = types;
}

internal ImmutableArray<AttributeData> GetAttributes(ISymbol symbol)
{
if (_attributes.TryGetValue(symbol, out var attributes))
{
return attributes;
}

attributes = symbol.GetAttributes();
_attributes.Add(symbol, attributes);

return attributes;
}

internal bool HasAttribute<T>(ISymbol symbol)
internal IEnumerable<AttributeData> GetAttributes<T>(ISymbol symbol)
where T : Attribute
{
var attributeSymbol = _types.Get(typeof(T));
return GetAttributes(symbol).Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol));
return GetAttributesCore(symbol)
.Where(x => SymbolEqualityComparer.Default.Equals(x.AttributeClass?.ConstructedFrom ?? x.AttributeClass, attributeSymbol));
}

internal bool HasAttribute<T>(ISymbol symbol)
where T : Attribute => GetAttributes<T>(symbol).Any();

internal IEnumerable<IMethodSymbol> GetAllMethods(ITypeSymbol symbol) => GetAllMembers(symbol).OfType<IMethodSymbol>();

internal IEnumerable<IMethodSymbol> GetAllMethods(ITypeSymbol symbol, string name) =>
Expand Down Expand Up @@ -81,6 +72,19 @@ internal IEnumerable<IMappableMember> GetMappableMembers(ITypeSymbol symbol, str

private IEnumerable<ISymbol> GetAllMembers(ITypeSymbol symbol, string name) => GetAllMembers(symbol).Where(x => name.Equals(x.Name));

private ImmutableArray<AttributeData> GetAttributesCore(ISymbol symbol)
{
if (_attributes.TryGetValue(symbol, out var attributes))
{
return attributes;
}

attributes = symbol.GetAttributes();
_attributes.Add(symbol, attributes);

return attributes;
}

private IEnumerable<ISymbol> GetAllMembersCore(ITypeSymbol symbol)
{
var members = symbol.GetMembers();
Expand All @@ -97,7 +101,7 @@ private IEnumerable<ISymbol> GetAllMembersCore(ITypeSymbol symbol)
private IEnumerable<IMappableMember> GetAllAccessibleMappableMembersCore(ITypeSymbol symbol)
{
return GetAllMembers(symbol)
.Where(x => !x.IsStatic && x.IsAccessible() && x.Kind is SymbolKind.Property or SymbolKind.Field)
.Where(x => x is { IsStatic: false, Kind: SymbolKind.Property or SymbolKind.Field } && x.IsAccessible())
.DistinctBy(x => x.Name)
.Select(MappableMember.Create)
.WhereNotNull();
Expand Down
4 changes: 2 additions & 2 deletions src/Riok.Mapperly/Descriptors/WellKnownTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ internal WellKnownTypes(Compilation compilation)
public ITypeSymbol GetArrayType(ITypeSymbol type) =>
_compilation.CreateArrayTypeSymbol(type, elementNullableAnnotation: type.NullableAnnotation).NonNullable();

public INamedTypeSymbol Get<T>() => Get(typeof(T).FullName);
public INamedTypeSymbol Get<T>() => Get(typeof(T));

public INamedTypeSymbol Get(Type type) =>
Get(type.FullName ?? throw new InvalidOperationException("Could not get name of type " + type));
Get($"{type.Namespace}.{type.Name}" ?? throw new InvalidOperationException("Could not get name of type " + type));

public INamedTypeSymbol Get(string typeFullName) =>
TryGet(typeFullName) ?? throw new InvalidOperationException("Could not get type " + typeFullName);
Expand Down

0 comments on commit b910c18

Please sign in to comment.