-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
322 additions
and
148 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Riok.Mapperly/Descriptors/Enumerables/CollectionInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
public record CollectionInfo( | ||
CollectionType Type, | ||
CollectionType ImplementedTypes, | ||
ITypeSymbol EnumeratedType, | ||
bool CountIsKnown, | ||
bool HasValidAddMethod, | ||
bool IsImmutableCollectionType | ||
) | ||
{ | ||
public (ITypeSymbol, ITypeSymbol)? GetDictionaryKeyValueTypes(MappingBuilderContext ctx, ITypeSymbol t) | ||
{ | ||
if (t.ImplementsGeneric(ctx.Types.Get(typeof(IDictionary<,>)), out var dictionaryImpl)) | ||
{ | ||
return (dictionaryImpl.TypeArguments[0], dictionaryImpl.TypeArguments[1]); | ||
} | ||
|
||
if (t.ImplementsGeneric(ctx.Types.Get(typeof(IReadOnlyDictionary<,>)), out var readOnlyDictionaryImpl)) | ||
{ | ||
return (readOnlyDictionaryImpl.TypeArguments[0], readOnlyDictionaryImpl.TypeArguments[1]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public (ITypeSymbol, ITypeSymbol)? GetEnumeratedKeyValueTypes(WellKnownTypes types) | ||
{ | ||
if ( | ||
EnumeratedType is not INamedTypeSymbol namedEnumeratedType | ||
|| !SymbolEqualityComparer.Default.Equals(namedEnumeratedType.ConstructedFrom, types.Get(typeof(KeyValuePair<,>))) | ||
) | ||
return null; | ||
|
||
return (namedEnumeratedType.TypeArguments[0], namedEnumeratedType.TypeArguments[1]); | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
src/Riok.Mapperly/Descriptors/Enumerables/CollectionInfoBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
public static class CollectionInfoBuilder | ||
{ | ||
private static readonly IReadOnlyDictionary<Type, CollectionType> _collectionTypeMappings = new Dictionary<Type, CollectionType> | ||
{ | ||
[typeof(List<>)] = CollectionType.List, | ||
[typeof(Stack<>)] = CollectionType.Stack, | ||
[typeof(Queue<>)] = CollectionType.Queue, | ||
[typeof(IReadOnlyCollection<>)] = CollectionType.IReadOnlyCollection, | ||
[typeof(IList<>)] = CollectionType.IList, | ||
[typeof(IReadOnlyList<>)] = CollectionType.IReadOnlyList, | ||
[typeof(ICollection<>)] = CollectionType.ICollection, | ||
[typeof(HashSet<>)] = CollectionType.HashSet, | ||
[typeof(SortedSet<>)] = CollectionType.SortedSet, | ||
[typeof(ISet<>)] = CollectionType.ISet, | ||
[typeof(IDictionary<,>)] = CollectionType.IDictionary, | ||
[typeof(IReadOnlyDictionary<,>)] = CollectionType.IReadOnlyDictionary, | ||
[typeof(Dictionary<,>)] = CollectionType.Dictionary, | ||
[typeof(ImmutableArray<>)] = CollectionType.ImmutableArray, | ||
[typeof(ImmutableList<>)] = CollectionType.ImmutableList, | ||
[typeof(IImmutableList<>)] = CollectionType.IImmutableList, | ||
[typeof(ImmutableHashSet<>)] = CollectionType.ImmutableHashSet, | ||
[typeof(IImmutableSet<>)] = CollectionType.IImmutableSet, | ||
[typeof(ImmutableSortedSet<>)] = CollectionType.ImmutableSortedSet, | ||
[typeof(ImmutableQueue<>)] = CollectionType.ImmutableQueue, | ||
[typeof(IImmutableQueue<>)] = CollectionType.IImmutableQueue, | ||
[typeof(ImmutableStack<>)] = CollectionType.ImmutableStack, | ||
[typeof(IImmutableStack<>)] = CollectionType.IImmutableStack, | ||
[typeof(IImmutableDictionary<,>)] = CollectionType.IImmutableDictionary, | ||
[typeof(ImmutableDictionary<,>)] = CollectionType.ImmutableDictionary, | ||
[typeof(IEnumerable<>)] = CollectionType.IEnumerable, | ||
}; | ||
|
||
private static readonly IReadOnlyDictionary<string, CollectionType> _newCollectionTypeMappings = new Dictionary<string, CollectionType> | ||
{ | ||
["System.Collections.Generic.IReadOnlySet"] = CollectionType.IReadOnlySet, | ||
}; | ||
|
||
public static CollectionInfos? Build(WellKnownTypes wellKnownTypes, ITypeSymbol source, ITypeSymbol target) | ||
{ | ||
var collectionTypes = GetCollectionTypeMappings(wellKnownTypes); | ||
|
||
if (Build(wellKnownTypes, collectionTypes, source) is not { } sourceInfo) | ||
return null; | ||
|
||
if (Build(wellKnownTypes, collectionTypes, target) is not { } targetInfo) | ||
return null; | ||
|
||
return new CollectionInfos(sourceInfo, targetInfo); | ||
} | ||
|
||
private static CollectionInfo? Build( | ||
WellKnownTypes wellKnownTypes, | ||
IReadOnlyCollection<(INamedTypeSymbol Type, CollectionType CollectionType)> collectionTypes, | ||
ITypeSymbol type | ||
) | ||
{ | ||
var enumeratedType = GetEnumeratedType(wellKnownTypes, type); | ||
if (enumeratedType == null) | ||
return null; | ||
|
||
var collectionType = GetCollectionType(collectionTypes, type); | ||
return new CollectionInfo( | ||
collectionType, | ||
GetImplementedCollectionTypes(collectionTypes, type), | ||
enumeratedType, | ||
IsCountKnown(wellKnownTypes, type), | ||
HasValidAddMethod(wellKnownTypes, type), | ||
IsImmutable(collectionType) | ||
); | ||
} | ||
|
||
private static ITypeSymbol? GetEnumeratedType(WellKnownTypes types, ITypeSymbol type) | ||
{ | ||
return type.ImplementsGeneric(types.Get(typeof(IEnumerable<>)), out var enumerableIntf) ? enumerableIntf.TypeArguments[0] : null; | ||
} | ||
|
||
private static bool HasValidAddMethod(WellKnownTypes types, ITypeSymbol t) | ||
{ | ||
return t.HasImplicitGenericImplementation(types.Get(typeof(ICollection<>)), nameof(ICollection<object>.Add)) | ||
|| t.HasImplicitGenericImplementation(types.Get(typeof(ISet<>)), nameof(ISet<object>.Add)); | ||
} | ||
|
||
private static bool IsCountKnown(WellKnownTypes types, ITypeSymbol t) | ||
{ | ||
var intType = types.Get<int>(); | ||
return t.GetAccessibleMappableMembers() | ||
.Any( | ||
x => | ||
x.Name is nameof(ICollection<object>.Count) or nameof(Array.Length) | ||
&& SymbolEqualityComparer.IncludeNullability.Equals(intType, x.Type) | ||
); | ||
} | ||
|
||
private static bool IsImmutable(CollectionType t) | ||
{ | ||
switch (t) | ||
{ | ||
case CollectionType.ImmutableArray: | ||
case CollectionType.ImmutableList: | ||
case CollectionType.IImmutableList: | ||
case CollectionType.ImmutableHashSet: | ||
case CollectionType.IImmutableSet: | ||
case CollectionType.ImmutableSortedSet: | ||
case CollectionType.ImmutableQueue: | ||
case CollectionType.IImmutableQueue: | ||
case CollectionType.ImmutableStack: | ||
case CollectionType.IImmutableStack: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
private static CollectionType GetCollectionType( | ||
IEnumerable<(INamedTypeSymbol Type, CollectionType CollectionType)> collectionTypes, | ||
ITypeSymbol type | ||
) | ||
{ | ||
if (type.IsArrayType()) | ||
return CollectionType.Array; | ||
|
||
foreach (var (collectionTypeSymbol, collectionType) in collectionTypes) | ||
{ | ||
if (SymbolEqualityComparer.Default.Equals(type.OriginalDefinition, collectionTypeSymbol)) | ||
return collectionType; | ||
} | ||
|
||
return CollectionType.None; | ||
} | ||
|
||
private static CollectionType GetImplementedCollectionTypes( | ||
IEnumerable<(INamedTypeSymbol Type, CollectionType CollectionType)> collectionTypes, | ||
ITypeSymbol type | ||
) | ||
{ | ||
var implementedCollectionTypes = type.IsArrayType() ? CollectionType.Array : CollectionType.None; | ||
|
||
foreach (var (collectionTypeSymbol, collectionType) in collectionTypes) | ||
{ | ||
if (type.ImplementsGeneric(collectionTypeSymbol, out _)) | ||
{ | ||
implementedCollectionTypes |= collectionType; | ||
} | ||
} | ||
|
||
return implementedCollectionTypes; | ||
} | ||
|
||
private static IReadOnlyCollection<(INamedTypeSymbol Type, CollectionType CollectionType)> GetCollectionTypeMappings( | ||
WellKnownTypes types | ||
) | ||
{ | ||
return _newCollectionTypeMappings | ||
.Select(x => (Type: types.TryGet(x.Key), CollectionType: x.Value)) | ||
.Where(x => x.Type != null)! | ||
.Concat(_collectionTypeMappings.Select(x => (types.Get(x.Key), x.Value))) | ||
.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
public record CollectionInfos(CollectionInfo Source, CollectionInfo Target); |
43 changes: 43 additions & 0 deletions
43
src/Riok.Mapperly/Descriptors/Enumerables/CollectionType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
[Flags] | ||
public enum CollectionType | ||
{ | ||
None = 0, | ||
Array = 1 << 0, | ||
IEnumerable = 1 << 1, | ||
|
||
// collections | ||
List = 1 << 2, | ||
Stack = 1 << 3, | ||
Queue = 1 << 4, | ||
IReadOnlyCollection = 1 << 5, | ||
IList = 1 << 6, | ||
IReadOnlyList = 1 << 7, | ||
ICollection = 1 << 8, | ||
|
||
// sets | ||
HashSet = 1 << 9, | ||
SortedSet = 1 << 10, | ||
IReadOnlySet = 1 << 11, | ||
ISet = 1 << 12, | ||
|
||
// dictionaries | ||
IDictionary = 1 << 13, | ||
IReadOnlyDictionary = 1 << 14, | ||
Dictionary = 1 << 15, | ||
|
||
// immutable | ||
ImmutableArray = 1 << 16, | ||
ImmutableList = 1 << 17, | ||
IImmutableList = 1 << 18, | ||
ImmutableHashSet = 1 << 19, | ||
IImmutableSet = 1 << 20, | ||
ImmutableSortedSet = 1 << 21, | ||
ImmutableQueue = 1 << 22, | ||
IImmutableQueue = 1 << 23, | ||
ImmutableStack = 1 << 24, | ||
IImmutableStack = 1 << 25, | ||
ImmutableDictionary = 1 << 26, | ||
IImmutableDictionary = 1 << 27, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.