/
DictionaryMappingBuilder.cs
203 lines (170 loc) · 7.67 KB
/
DictionaryMappingBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using Microsoft.CodeAnalysis;
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Descriptors.Enumerables;
using Riok.Mapperly.Descriptors.Enumerables.EnsureCapacity;
using Riok.Mapperly.Descriptors.Mappings;
using Riok.Mapperly.Descriptors.Mappings.ExistingTarget;
using Riok.Mapperly.Diagnostics;
using Riok.Mapperly.Helpers;
namespace Riok.Mapperly.Descriptors.MappingBuilders;
public static class DictionaryMappingBuilder
{
private const string SetterIndexerPropertyName = "set_Item";
private const string ToImmutableDictionaryMethodName = "global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary";
private const string ToImmutableSortedDictionaryMethodName =
"global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary";
public static INewInstanceMapping? TryBuildMapping(MappingBuilderContext ctx)
{
if (!ctx.IsConversionEnabled(MappingConversionType.Dictionary))
return null;
if (ctx.CollectionInfos == null)
return null;
if (BuildKeyValueMapping(ctx) is not var (keyMapping, valueMapping))
return null;
// target is of type IDictionary<,>, IReadOnlyDictionary<,> or Dictionary<,>.
// The constructed type should be Dictionary<,>
if (
ctx.CollectionInfos?.Target.CollectionType
is CollectionType.Dictionary
or CollectionType.IDictionary
or CollectionType.IReadOnlyDictionary
)
{
if (TryGetFromEnumerable(ctx, keyMapping, valueMapping) is { } toDictionary)
return toDictionary;
var targetDictionarySymbol = ctx.Types.Get(typeof(Dictionary<,>)).Construct(keyMapping.TargetType, valueMapping.TargetType);
ctx.ObjectFactories.TryFindObjectFactory(ctx.Source, ctx.Target, out var dictionaryObjectFactory);
return new ForEachSetDictionaryMapping(
ctx.Source,
ctx.Target,
keyMapping,
valueMapping,
ctx.CollectionInfos.Source.CountIsKnown,
targetDictionarySymbol,
dictionaryObjectFactory
);
}
// if target is an immutable dictionary then use LinqDictionaryMapper
var immutableLinqMapping = ResolveImmutableCollectMethod(ctx, keyMapping, valueMapping);
if (immutableLinqMapping != null)
return immutableLinqMapping;
// the target is not a well known dictionary type
// it should have a an object factory or a parameterless public ctor
if (
!ctx.ObjectFactories.TryFindObjectFactory(ctx.Source, ctx.Target, out var objectFactory)
&& !ctx.SymbolAccessor.HasDirectlyAccessibleParameterlessConstructor(ctx.Target)
)
{
ctx.ReportDiagnostic(DiagnosticDescriptors.NoParameterlessConstructorFound, ctx.Target);
return null;
}
if (!ctx.CollectionInfos!.Target.ImplementedTypes.HasFlag(CollectionType.IDictionary))
return null;
var ensureCapacityStatement = EnsureCapacityBuilder.TryBuildEnsureCapacity(ctx);
return new ForEachSetDictionaryMapping(
ctx.Source,
ctx.Target,
keyMapping,
valueMapping,
false,
objectFactory: objectFactory,
explicitCast: GetExplicitIndexer(ctx),
ensureCapacity: ensureCapacityStatement
);
}
public static IExistingTargetMapping? TryBuildExistingTargetMapping(MappingBuilderContext ctx)
{
if (!ctx.IsConversionEnabled(MappingConversionType.Dictionary))
return null;
if (ctx.CollectionInfos == null)
return null;
if (!ctx.CollectionInfos.Target.ImplementedTypes.HasFlag(CollectionType.IDictionary))
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.CollectionInfos.Target.ImplementedTypes.HasFlag(CollectionType.IImmutableDictionary))
{
ctx.ReportDiagnostic(DiagnosticDescriptors.CannotMapToReadOnlyMember);
return null;
}
// add values to dictionary by setting key values in a foreach loop
var ensureCapacityStatement = EnsureCapacityBuilder.TryBuildEnsureCapacity(ctx);
return new ForEachSetDictionaryExistingTargetMapping(
ctx.Source,
ctx.Target,
keyMapping,
valueMapping,
explicitCast: GetExplicitIndexer(ctx),
ensureCapacity: ensureCapacityStatement
);
}
private static (INewInstanceMapping, INewInstanceMapping)? BuildKeyValueMapping(MappingBuilderContext ctx)
{
if (ctx.CollectionInfos!.Target.GetDictionaryKeyValueTypes(ctx) is not var (targetKeyType, targetValueType))
return null;
if (ctx.CollectionInfos.Source.GetEnumeratedKeyValueTypes(ctx.Types) is not var (sourceKeyType, sourceValueType))
return null;
var keyMapping = ctx.FindOrBuildMapping(sourceKeyType, targetKeyType);
if (keyMapping == null)
return null;
var valueMapping = ctx.FindOrBuildMapping(sourceValueType, targetValueType);
if (valueMapping == null)
return null;
return (keyMapping, valueMapping);
}
private static INewInstanceMapping? TryGetFromEnumerable(
MappingBuilderContext ctx,
INewInstanceMapping keyMapping,
INewInstanceMapping valueMapping
)
{
if (!keyMapping.IsSynthetic || !valueMapping.IsSynthetic || keyMapping.TargetType.IsNullable())
return null;
// use .NET Core 2+ Dictionary constructor if value and key mapping is synthetic
var enumerableType = ctx.Types.Get(typeof(IEnumerable<>));
var dictionaryType = ctx.Types.Get(typeof(Dictionary<,>));
var fromEnumerableCtor = dictionaryType.Constructors.FirstOrDefault(
x =>
x.Parameters.Length == 1
&& SymbolEqualityComparer.Default.Equals(((INamedTypeSymbol)x.Parameters[0].Type).ConstructedFrom, enumerableType)
);
if (fromEnumerableCtor != null)
{
var constructedDictionary = dictionaryType
.Construct(keyMapping.TargetType, valueMapping.TargetType)
.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
return new CtorMapping(ctx.Source, constructedDictionary);
}
return null;
}
private static INamedTypeSymbol? GetExplicitIndexer(MappingBuilderContext ctx)
{
if (
ctx.Target.ImplementsGeneric(
ctx.Types.Get(typeof(IDictionary<,>)),
SetterIndexerPropertyName,
out var typedInter,
out var isExplicit
) && !isExplicit
)
return null;
return typedInter;
}
private static LinqDictionaryMapping? ResolveImmutableCollectMethod(
MappingBuilderContext ctx,
INewInstanceMapping keyMapping,
INewInstanceMapping valueMapping
)
{
return ctx.CollectionInfos!.Target.CollectionType switch
{
CollectionType.ImmutableSortedDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableSortedDictionaryMethodName, keyMapping, valueMapping),
CollectionType.ImmutableDictionary
or CollectionType.IImmutableDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableDictionaryMethodName, keyMapping, valueMapping),
_ => null,
};
}
}