-
Notifications
You must be signed in to change notification settings - Fork 819
/
DynamicTypeInfoResolver.cs
132 lines (105 loc) · 6.77 KB
/
DynamicTypeInfoResolver.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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Npgsql.Internal.Postgres;
using Npgsql.PostgresTypes;
namespace Npgsql.Internal;
[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 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;
static readonly MethodInfo AddTypeMethodInfo = typeof(TypeInfoMappingCollection).GetMethod(nameof(TypeInfoMappingCollection.AddType),
new[] { typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddArrayTypeMethodInfo = typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddArrayType), new[] { typeof(string) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddStructTypeMethodInfo = typeof(TypeInfoMappingCollection).GetMethod(nameof(TypeInfoMappingCollection.AddStructType),
new[] { typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddStructArrayTypeMethodInfo = typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddStructArrayType), new[] { typeof(string) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddResolverTypeMethodInfo = typeof(TypeInfoMappingCollection).GetMethod(
nameof(TypeInfoMappingCollection.AddResolverType),
new[] { typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddResolverArrayTypeMethodInfo = typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverArrayType), new[] { typeof(string) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddResolverStructTypeMethodInfo = typeof(TypeInfoMappingCollection).GetMethod(
nameof(TypeInfoMappingCollection.AddResolverStructType),
new[] { typeof(string), typeof(TypeInfoFactory), typeof(Func<TypeInfoMapping, TypeInfoMapping>) }) ?? throw new NullReferenceException();
static readonly MethodInfo AddResolverStructArrayTypeMethodInfo = typeof(TypeInfoMappingCollection)
.GetMethod(nameof(TypeInfoMappingCollection.AddResolverStructArrayType), new[] { typeof(string) }) ?? throw new NullReferenceException();
internal DynamicMappingCollection(TypeInfoMappingCollection? baseCollection = null)
{
if (baseCollection is not null)
_mappings = new(baseCollection);
}
public DynamicMappingCollection AddMapping(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.");
(type.IsValueType ? AddStructTypeMethodInfo : AddTypeMethodInfo)
.MakeGenericMethod(type).Invoke(_mappings ??= new(), new object?[]
{
dataTypeName,
factory,
configureMapping
});
return this;
}
public DynamicMappingCollection AddArrayMapping(Type elementType, string dataTypeName)
{
(elementType.IsValueType ? AddStructArrayTypeMethodInfo : AddArrayTypeMethodInfo)
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), new object?[] { dataTypeName });
return this;
}
public DynamicMappingCollection AddResolverMapping(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");
(type.IsValueType ? AddResolverStructTypeMethodInfo : AddResolverTypeMethodInfo)
.MakeGenericMethod(type).Invoke(_mappings ??= new(), new object?[]
{
dataTypeName,
factory,
configureMapping
});
return this;
}
public DynamicMappingCollection AddResolverArrayMapping(Type elementType, string dataTypeName)
{
(elementType.IsValueType ? AddResolverStructArrayTypeMethodInfo : AddResolverArrayTypeMethodInfo)
.MakeGenericMethod(elementType).Invoke(_mappings ??= new(), new object?[] { 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>());
}
}