Skip to content

Commit

Permalink
fix: remove fields from WellKnownTypes, share WellKnownTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jun 3, 2023
1 parent 222ff30 commit 5ab47d0
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 190 deletions.
2 changes: 0 additions & 2 deletions src/Riok.Mapperly/Configuration/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public static IEnumerable<TData> Access<TAttribute, TData>(WellKnownTypes knownT
{
var attrType = typeof(TAttribute);
var attrSymbol = knownTypes.GetTypeSymbol($"{attrType.Namespace}.{attrType.Name}");
if (attrSymbol == null)
yield break;

var attrDatas = symbol
.GetAttributes()
Expand Down
7 changes: 4 additions & 3 deletions src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Abstractions.ReferenceHandling;
using Riok.Mapperly.Descriptors.MappingBodyBuilders;
using Riok.Mapperly.Descriptors.MappingBuilders;
using Riok.Mapperly.Descriptors.ObjectFactories;
Expand All @@ -22,10 +23,10 @@ public DescriptorBuilder(
SourceProductionContext sourceContext,
Compilation compilation,
ClassDeclarationSyntax mapperSyntax,
INamedTypeSymbol mapperSymbol
INamedTypeSymbol mapperSymbol,
WellKnownTypes wellKnownTypes
)
{
var wellKnownTypes = new WellKnownTypes(compilation);
_mapperDescriptor = new MapperDescriptor(mapperSyntax, mapperSymbol, _methodNameBuilder);
_mappingBodyBuilder = new MappingBodyBuilder(_mappings);
_builderContext = new SimpleMappingBuilderContext(
Expand Down Expand Up @@ -96,7 +97,7 @@ private void BuildReferenceHandlingParameters()

foreach (var methodMapping in _mappings.MethodMappings)
{
methodMapping.EnableReferenceHandling(_builderContext.Types.IReferenceHandler);
methodMapping.EnableReferenceHandling(_builderContext.Types.GetTypeSymbol<IReferenceHandler>());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public static class EnsureCapacityBuilder
if (TryGetNonEnumeratedCount(sourceType, types, out var sourceSizeProperty))
return new EnsureCapacityMember(targetSizeProperty, sourceSizeProperty);

sourceType.ImplementsGeneric(types.IEnumerableT, out var iEnumerable);
sourceType.ImplementsGeneric(types.GetTypeSymbol(typeof(IEnumerable<>)), out var iEnumerable);

var nonEnumeratedCountMethod = types.Enumerable
var nonEnumeratedCountMethod = types
.GetTypeSymbol(typeof(Enumerable))
.GetMembers(TryGetNonEnumeratedCountMethodName)
.OfType<IMethodSymbol>()
.FirstOrDefault(
Expand All @@ -59,13 +60,19 @@ private static bool TryGetNonEnumeratedCount(ITypeSymbol value, WellKnownTypes t
return true;
}

if (value.ImplementsGeneric(types.ICollectionT, CountPropertyName, out _, out var hasCollectionCount) && !hasCollectionCount)
if (
value.ImplementsGeneric(types.GetTypeSymbol(typeof(ICollection<>)), CountPropertyName, out _, out var hasCollectionCount)
&& !hasCollectionCount
)
{
expression = CountPropertyName;
return true;
}

if (value.ImplementsGeneric(types.IReadOnlyCollectionT, CountPropertyName, out _, out var hasReadOnlyCount) && !hasReadOnlyCount)
if (
value.ImplementsGeneric(types.GetTypeSymbol(typeof(IReadOnlyCollection<>)), CountPropertyName, out _, out var hasReadOnlyCount)
&& !hasReadOnlyCount
)
{
expression = CountPropertyName;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ private static void BuildConstructorMapping(INewInstanceBuilderContext<IMapping>
// ctors annotated with [Obsolete] are considered last unless they have a MapperConstructor attribute set
var ctorCandidates = namedTargetType.InstanceConstructors
.Where(ctor => ctor.IsAccessible())
.OrderByDescending(x => x.HasAttribute(ctx.BuilderContext.Types.MapperConstructorAttribute))
.ThenBy(x => x.HasAttribute(ctx.BuilderContext.Types.ObsoleteAttribute))
.OrderByDescending(x => x.HasAttribute(ctx.BuilderContext.Types.GetTypeSymbol(typeof(MapperConstructorAttribute))))
.ThenBy(x => x.HasAttribute(ctx.BuilderContext.Types.GetTypeSymbol(typeof(ObsoleteAttribute))))
.ThenByDescending(x => x.Parameters.Length == 0)
.ThenByDescending(x => x.Parameters.Length);
foreach (var ctorCandidate in ctorCandidates)
{
if (!TryBuildConstructorMapping(ctx, ctorCandidate, out var mappedTargetMemberNames, out var constructorParameterMappings))
{
if (ctorCandidate.HasAttribute(ctx.BuilderContext.Types.MapperConstructorAttribute))
if (ctorCandidate.HasAttribute(ctx.BuilderContext.Types.GetTypeSymbol(typeof(MapperConstructorAttribute))))
{
ctx.BuilderContext.ReportDiagnostic(
DiagnosticDescriptors.CannotMapToConfiguredConstructor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public static class DictionaryMappingBuilder
.GetAllProperties(CountPropertyName)
.Any(x => !x.IsStatic && !x.IsIndexer && !x.IsWriteOnly && x.Type.SpecialType == SpecialType.System_Int32);

var targetDictionarySymbol = ctx.Types.DictionaryT.Construct(keyMapping.TargetType, valueMapping.TargetType);
var targetDictionarySymbol = ctx.Types
.GetTypeSymbol(typeof(Dictionary<,>))
.Construct(keyMapping.TargetType, valueMapping.TargetType);
ctx.ObjectFactories.TryFindObjectFactory(ctx.Source, ctx.Target, out var dictionaryObjectFactory);
return new ForEachSetDictionaryMapping(
ctx.Source,
Expand Down Expand Up @@ -62,7 +64,7 @@ public static class DictionaryMappingBuilder
return null;
}

if (!ctx.Target.ImplementsGeneric(ctx.Types.IDictionaryT, out _))
if (!ctx.Target.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IDictionary<,>)), out _))
return null;

var ensureCapacityStatement = EnsureCapacityBuilder.TryBuildEnsureCapacity(ctx.Source, ctx.Target, ctx.Types);
Expand All @@ -84,14 +86,14 @@ public static class DictionaryMappingBuilder
if (!ctx.IsConversionEnabled(MappingConversionType.Dictionary))
return null;

if (!ctx.Target.ImplementsGeneric(ctx.Types.IDictionaryT, out _))
if (!ctx.Target.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IDictionary<,>)), out _))
return null;

if (BuildKeyValueMapping(ctx) is not var (keyMapping, valueMapping))
return null;

// if target is an immutable dictionary then don't create a foreach loop
if (ctx.Target.OriginalDefinition.ImplementsGeneric(ctx.Types.IImmutableDictionaryT, out _))
if (ctx.Target.OriginalDefinition.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IImmutableDictionary<,>)), out _))
{
ctx.ReportDiagnostic(DiagnosticDescriptors.CannotMapToReadOnlyMember);
return null;
Expand Down Expand Up @@ -134,19 +136,19 @@ private static bool IsDictionaryType(MappingBuilderContext ctx, ITypeSymbol symb
if (symbol is not INamedTypeSymbol namedSymbol)
return false;

return SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.DictionaryT)
|| SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.IDictionaryT)
|| SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.IReadOnlyDictionaryT);
return SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.GetTypeSymbol(typeof(Dictionary<,>)))
|| SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.GetTypeSymbol(typeof(IDictionary<,>)))
|| SymbolEqualityComparer.Default.Equals(namedSymbol.ConstructedFrom, ctx.Types.GetTypeSymbol(typeof(IReadOnlyDictionary<,>)));
}

private static (ITypeSymbol, ITypeSymbol)? GetDictionaryKeyValueTypes(MappingBuilderContext ctx, ITypeSymbol t)
{
if (t.ImplementsGeneric(ctx.Types.IDictionaryT, out var dictionaryImpl))
if (t.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IDictionary<,>)), out var dictionaryImpl))
{
return (dictionaryImpl.TypeArguments[0], dictionaryImpl.TypeArguments[1]);
}

if (t.ImplementsGeneric(ctx.Types.IReadOnlyDictionaryT, out var readOnlyDictionaryImpl))
if (t.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IReadOnlyDictionary<,>)), out var readOnlyDictionaryImpl))
{
return (readOnlyDictionaryImpl.TypeArguments[0], readOnlyDictionaryImpl.TypeArguments[1]);
}
Expand All @@ -156,13 +158,13 @@ private static (ITypeSymbol, ITypeSymbol)? GetDictionaryKeyValueTypes(MappingBui

private static (ITypeSymbol, ITypeSymbol)? GetEnumerableKeyValueTypes(MappingBuilderContext ctx, ITypeSymbol t)
{
if (!t.ImplementsGeneric(ctx.Types.IEnumerableT, out var enumerableImpl))
if (!t.ImplementsGeneric(ctx.Types.GetTypeSymbol(typeof(IEnumerable<>)), out var enumerableImpl))
return null;

if (enumerableImpl.TypeArguments[0] is not INamedTypeSymbol enumeratedType)
return null;

if (!SymbolEqualityComparer.Default.Equals(enumeratedType.ConstructedFrom, ctx.Types.KeyValuePairT))
if (!SymbolEqualityComparer.Default.Equals(enumeratedType.ConstructedFrom, ctx.Types.GetTypeSymbol(typeof(KeyValuePair<,>))))
return null;

return (enumeratedType.TypeArguments[0], enumeratedType.TypeArguments[1]);
Expand All @@ -171,8 +173,12 @@ private static (ITypeSymbol, ITypeSymbol)? GetEnumerableKeyValueTypes(MappingBui
private static INamedTypeSymbol? GetExplicitIndexer(MappingBuilderContext ctx)
{
if (
ctx.Target.ImplementsGeneric(ctx.Types.IDictionaryT, SetterIndexerPropertyName, out var typedInter, out var isExplicit)
&& !isExplicit
ctx.Target.ImplementsGeneric(
ctx.Types.GetTypeSymbol(typeof(IDictionary<,>)),
SetterIndexerPropertyName,
out var typedInter,
out var isExplicit
) && !isExplicit
)
return null;

Expand All @@ -185,24 +191,29 @@ private static (ITypeSymbol, ITypeSymbol)? GetEnumerableKeyValueTypes(MappingBui
ITypeMapping valueMapping
)
{
if (SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.ImmutableSortedDictionaryT))
if (
SymbolEqualityComparer.Default.Equals(
ctx.Target.OriginalDefinition,
ctx.Types.GetTypeSymbol(typeof(ImmutableSortedDictionary<,>))
)
)
return new LinqDicitonaryMapping(
ctx.Source,
ctx.Target,
ctx.Types.ImmutableSortedDictionary.GetStaticGenericMethod(ToImmutableSortedDictionaryMethodName)!,
ctx.Types.GetTypeSymbol(typeof(ImmutableSortedDictionary)).GetStaticGenericMethod(ToImmutableSortedDictionaryMethodName)!,
keyMapping,
valueMapping
);

// if target is an ImmutableDictionary or IImmutableDictionary
if (
SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.IImmutableDictionaryT)
|| SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.ImmutableDictionaryT)
SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.GetTypeSymbol(typeof(IImmutableDictionary<,>)))
|| SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.GetTypeSymbol(typeof(ImmutableDictionary<,>)))
)
return new LinqDicitonaryMapping(
ctx.Source,
ctx.Target,
ctx.Types.ImmutableDictionary.GetStaticGenericMethod(ToImmutableDictionaryMethodName)!,
ctx.Types.GetTypeSymbol(typeof(ImmutableDictionary)).GetStaticGenericMethod(ToImmutableDictionaryMethodName)!,
keyMapping,
valueMapping
);
Expand Down
Loading

0 comments on commit 5ab47d0

Please sign in to comment.