-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathDynamicTypeInfoResolver.cs
More file actions
140 lines (121 loc) · 6.94 KB
/
DynamicTypeInfoResolver.cs
File metadata and controls
140 lines (121 loc) · 6.94 KB
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
using System;
using System.Diagnostics.CodeAnalysis;
using Npgsql.Internal.Postgres;
using Npgsql.PostgresTypes;
namespace Npgsql.Internal;
[Experimental(NpgsqlDiagnostics.ConvertersExperimental)]
[RequiresDynamicCode("A dynamic type info resolver may need to construct a generic converter for a statically unknown type.")]
public abstract class DynamicTypeInfoResolver : IPgTypeInfoResolver
{
public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options)
{
if (dataTypeName is null)
return null;
var context = GetMappings(type, dataTypeName.GetValueOrDefault(), options);
return context?.Find(type, dataTypeName.GetValueOrDefault(), options);
}
protected static DynamicMappingCollection CreateCollection(TypeInfoMappingCollection? baseCollection = null) => new(baseCollection);
protected static bool IsTypeOrNullableOfType(Type type, Func<Type, bool> predicate, out Type matchedType)
{
matchedType = Nullable.GetUnderlyingType(type) ?? type;
return predicate(matchedType);
}
protected static bool IsArrayLikeType(Type type, [NotNullWhen(true)]out Type? elementType) => TypeInfoMappingCollection.IsArrayLikeType(type, out elementType);
protected static bool IsArrayDataTypeName(DataTypeName dataTypeName, PgSerializerOptions options, out DataTypeName elementDataTypeName)
{
if (options.DatabaseInfo.GetPostgresType(dataTypeName) is PostgresArrayType arrayType)
{
elementDataTypeName = arrayType.Element.DataTypeName;
return true;
}
elementDataTypeName = default;
return false;
}
protected abstract DynamicMappingCollection? GetMappings(Type? type, DataTypeName dataTypeName, PgSerializerOptions options);
[RequiresDynamicCode("A dynamic type info resolver may need to construct a generic converter for a statically unknown type.")]
protected class DynamicMappingCollection
{
TypeInfoMappingCollection? _mappings;
internal DynamicMappingCollection(TypeInfoMappingCollection? baseCollection = null)
{
if (baseCollection is not null)
_mappings = new(baseCollection);
}
public DynamicMappingCollection AddMapping([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]Type type, string dataTypeName, TypeInfoFactory factory, Func<TypeInfoMapping, TypeInfoMapping>? configureMapping = null)
{
if (type.IsValueType && Nullable.GetUnderlyingType(type) is not null)
throw new NotSupportedException("Mapping nullable types is not supported, map its underlying type instead to get both.");
if (type.IsValueType)
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddStructType), [typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>)])!
.MakeGenericMethod(type).Invoke(_mappings ??= new(),
[
dataTypeName,
factory,
configureMapping
]);
else
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddType), [typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>)])!
.MakeGenericMethod(type).Invoke(_mappings ??= new(),
[
dataTypeName,
factory,
configureMapping
]);
return this;
}
public DynamicMappingCollection AddArrayMapping([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]Type elementType, string dataTypeName)
{
if (elementType.IsValueType)
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddStructArrayType), [typeof(string)])!
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), [dataTypeName]);
else
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddArrayType), [typeof(string)])!
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), [dataTypeName]);
return this;
}
public DynamicMappingCollection AddResolverMapping([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]Type type, string dataTypeName, TypeInfoFactory factory, Func<TypeInfoMapping, TypeInfoMapping>? configureMapping = null)
{
if (type.IsValueType && Nullable.GetUnderlyingType(type) is not null)
throw new NotSupportedException("Mapping nullable types is not supported");
if (type.IsValueType)
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverStructType), [typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>)])!
.MakeGenericMethod(type).Invoke(_mappings ??= new(),
[
dataTypeName,
factory,
configureMapping
]);
else
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverType), [typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>)])!
.MakeGenericMethod(type).Invoke(_mappings ??= new(),
[
dataTypeName,
factory,
configureMapping
]);
return this;
}
public DynamicMappingCollection AddResolverArrayMapping([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]Type elementType, string dataTypeName)
{
if (elementType.IsValueType)
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverStructArrayType), [typeof(string)])!
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), [dataTypeName]);
else
typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverArrayType), [typeof(string)])!
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), [dataTypeName]);
return this;
}
internal PgTypeInfo? Find(Type? type, DataTypeName dataTypeName, PgSerializerOptions options)
=> _mappings?.Find(type, dataTypeName, options);
public TypeInfoMappingCollection ToTypeInfoMappingCollection()
=> new(_mappings?.Items ?? Array.Empty<TypeInfoMapping>());
}
}