-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not generalise enumerable dictionary value types (#1155)
- Loading branch information
Showing
9 changed files
with
196 additions
and
65 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
public record DictionaryInfo(CollectionInfo Collection, ITypeSymbol Key, ITypeSymbol Value); |
65 changes: 65 additions & 0 deletions
65
src/Riok.Mapperly/Descriptors/Enumerables/DictionaryInfoBuilder.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,65 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.Enumerables; | ||
|
||
public static class DictionaryInfoBuilder | ||
{ | ||
public static DictionaryInfos? Build(WellKnownTypes types, CollectionInfos? collectionInfos) | ||
{ | ||
if (collectionInfos == null) | ||
return null; | ||
|
||
var source = BuildSource(types, collectionInfos); | ||
if (source == null) | ||
return null; | ||
|
||
var target = BuildTarget(types, collectionInfos); | ||
if (target == null) | ||
return null; | ||
|
||
return new DictionaryInfos(source, target); | ||
} | ||
|
||
private static DictionaryInfo? BuildSource(WellKnownTypes types, CollectionInfos infos) | ||
{ | ||
if (GetEnumeratedKeyValueTypes(types, infos.Source) is not var (key, value)) | ||
return null; | ||
|
||
return new DictionaryInfo(infos.Source, key, value); | ||
} | ||
|
||
private static DictionaryInfo? BuildTarget(WellKnownTypes types, CollectionInfos infos) | ||
{ | ||
if (GetDictionaryKeyValueTypes(types, infos.Target) is not var (key, value)) | ||
return null; | ||
|
||
return new DictionaryInfo(infos.Target, key, value); | ||
} | ||
|
||
private static (ITypeSymbol, ITypeSymbol)? GetDictionaryKeyValueTypes(WellKnownTypes types, CollectionInfo info) | ||
{ | ||
if (info.Type.ImplementsGeneric(types.Get(typeof(IDictionary<,>)), out var dictionaryImpl)) | ||
{ | ||
return (dictionaryImpl.TypeArguments[0], dictionaryImpl.TypeArguments[1]); | ||
} | ||
|
||
if (info.Type.ImplementsGeneric(types.Get(typeof(IReadOnlyDictionary<,>)), out var readOnlyDictionaryImpl)) | ||
{ | ||
return (readOnlyDictionaryImpl.TypeArguments[0], readOnlyDictionaryImpl.TypeArguments[1]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static (ITypeSymbol, ITypeSymbol)? GetEnumeratedKeyValueTypes(WellKnownTypes types, CollectionInfo info) | ||
{ | ||
if ( | ||
info.EnumeratedType is not INamedTypeSymbol namedEnumeratedType | ||
|| !SymbolEqualityComparer.Default.Equals(namedEnumeratedType.ConstructedFrom, types.Get(typeof(KeyValuePair<,>))) | ||
) | ||
return null; | ||
|
||
return (namedEnumeratedType.TypeArguments[0], namedEnumeratedType.TypeArguments[1]); | ||
} | ||
} |
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 DictionaryInfos(DictionaryInfo Source, DictionaryInfo Target); |
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
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
35 changes: 35 additions & 0 deletions
35
...ok.Mapperly.Tests/_snapshots/DictionaryTest.CustomDictionaryWithList#Mapper.g.verified.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,35 @@ | ||
//HintName: Mapper.g.cs | ||
// <auto-generated /> | ||
#nullable enable | ||
public partial class Mapper | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private partial global::B Map(global::A source) | ||
{ | ||
var target = new global::B(); | ||
target.EnsureCapacity(source.Count + target.Count); | ||
foreach (var item in source) | ||
{ | ||
target[item.Key] = MapToList(item.Value); | ||
} | ||
return target; | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private global::D MapToD(global::C source) | ||
{ | ||
var target = new global::D(source.Value); | ||
return target; | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private global::System.Collections.Generic.List<global::D> MapToList(global::System.Collections.Generic.IReadOnlyCollection<global::C> source) | ||
{ | ||
var target = new global::System.Collections.Generic.List<global::D>(source.Count); | ||
foreach (var item in source) | ||
{ | ||
target.Add(MapToD(item)); | ||
} | ||
return target; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
test/Riok.Mapperly.Tests/_snapshots/DictionaryTest.DictionaryWithList#Mapper.g.verified.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,41 @@ | ||
//HintName: Mapper.g.cs | ||
// <auto-generated /> | ||
#nullable enable | ||
public partial class Mapper | ||
{ | ||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private partial global::B Map(global::A source) | ||
{ | ||
var target = new global::B(MapToDictionary(source.Dict)); | ||
return target; | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private global::D MapToD(global::C source) | ||
{ | ||
var target = new global::D(source.Value); | ||
return target; | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private global::System.Collections.Generic.List<global::D> MapToList(global::System.Collections.Generic.IReadOnlyCollection<global::C> source) | ||
{ | ||
var target = new global::System.Collections.Generic.List<global::D>(source.Count); | ||
foreach (var item in source) | ||
{ | ||
target.Add(MapToD(item)); | ||
} | ||
return target; | ||
} | ||
|
||
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] | ||
private global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<global::D>> MapToDictionary(global::System.Collections.Generic.IReadOnlyDictionary<int, global::System.Collections.Generic.List<global::C>> source) | ||
{ | ||
var target = new global::System.Collections.Generic.Dictionary<int, global::System.Collections.Generic.List<global::D>>(source.Count); | ||
foreach (var item in source) | ||
{ | ||
target[item.Key] = MapToList(item.Value); | ||
} | ||
return target; | ||
} | ||
} |