-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
AttributeConventionPack.cs
208 lines (186 loc) · 8.76 KB
/
AttributeConventionPack.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
204
205
206
207
208
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.Bson.Serialization.Conventions
{
/// <summary>
/// Convention pack for applying attributes.
/// </summary>
public class AttributeConventionPack : IConventionPack
{
// private static fields
private static readonly AttributeConventionPack __attributeConventionPack = new AttributeConventionPack();
// private fields
private readonly AttributeConvention _attributeConvention;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="AttributeConventionPack" /> class.
/// </summary>
private AttributeConventionPack()
{
_attributeConvention = new AttributeConvention();
}
// public static properties
/// <summary>
/// Gets the instance.
/// </summary>
public static IConventionPack Instance
{
get { return __attributeConventionPack; }
}
// public properties
/// <summary>
/// Gets the conventions.
/// </summary>
public IEnumerable<IConvention> Conventions
{
get { yield return _attributeConvention; }
}
// nested classes
private class AttributeConvention : ConventionBase, IClassMapConvention, ICreatorMapConvention, IMemberMapConvention, IPostProcessingConvention
{
// public methods
public void Apply(BsonClassMap classMap)
{
foreach (var attribute in classMap.ClassType.GetTypeInfo().GetCustomAttributes(inherit: false).OfType<IBsonClassMapAttribute>())
{
attribute.Apply(classMap);
}
OptInMembersWithBsonMemberMapModifierAttribute(classMap);
OptInMembersWithBsonCreatorMapModifierAttribute(classMap);
IgnoreMembersWithBsonIgnoreAttribute(classMap);
ThrowForDuplicateMemberMapAttributes(classMap);
}
public void Apply(BsonCreatorMap creatorMap)
{
if (creatorMap.MemberInfo != null)
{
foreach (var attribute in creatorMap.MemberInfo.GetCustomAttributes(inherit: false).OfType<IBsonCreatorMapAttribute>())
{
attribute.Apply(creatorMap);
}
}
}
public void Apply(BsonMemberMap memberMap)
{
var attributes = memberMap.MemberInfo.GetCustomAttributes(inherit: false).OfType<IBsonMemberMapAttribute>();
var groupings = attributes.GroupBy(a => (a is BsonSerializerAttribute) ? 1 : 2);
foreach (var grouping in groupings.OrderBy(g => g.Key))
{
foreach (var attribute in grouping)
{
attribute.Apply(memberMap);
}
}
}
public void PostProcess(BsonClassMap classMap)
{
foreach (var attribute in classMap.ClassType.GetTypeInfo().GetCustomAttributes(inherit: false).OfType<IBsonPostProcessingAttribute>())
{
attribute.PostProcess(classMap);
}
}
// private methods
private bool AllowsDuplicate(Type type)
{
var usageAttribute = type.GetTypeInfo().GetCustomAttributes(inherit: true)
.OfType<BsonMemberMapAttributeUsageAttribute>()
.SingleOrDefault();
return usageAttribute == null || usageAttribute.AllowMultipleMembers;
}
private void OptInMembersWithBsonCreatorMapModifierAttribute(BsonClassMap classMap)
{
TypeInfo classTypeInfo = classMap.ClassType.GetTypeInfo();
// let other constructors opt-in if they have any IBsonCreatorMapAttribute attributes
foreach (var constructorInfo in classTypeInfo.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
var hasAttribute = constructorInfo.GetCustomAttributes(inherit: false).OfType<IBsonCreatorMapAttribute>().Any();
if (hasAttribute)
{
classMap.MapConstructor(constructorInfo);
}
}
// let other static factory methods opt-in if they have any IBsonCreatorMapAttribute attributes
foreach (var methodInfo in classTypeInfo.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
var hasAttribute = methodInfo.GetCustomAttributes(inherit: false).OfType<IBsonCreatorMapAttribute>().Any();
if (hasAttribute)
{
classMap.MapFactoryMethod(methodInfo);
}
}
}
private void OptInMembersWithBsonMemberMapModifierAttribute(BsonClassMap classMap)
{
var classTypeInfo = classMap.ClassType.GetTypeInfo();
// let other fields opt-in if they have any IBsonMemberMapAttribute attributes
foreach (var fieldInfo in classTypeInfo.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
var hasAttribute = fieldInfo.GetCustomAttributes(inherit: false).OfType<IBsonMemberMapAttribute>().Any();
if (hasAttribute)
{
classMap.MapMember(fieldInfo);
}
}
// let other properties opt-in if they have any IBsonMemberMapAttribute attributes
foreach (var propertyInfo in classTypeInfo.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
{
var hasAttribute = propertyInfo.GetCustomAttributes(inherit: false).OfType<IBsonMemberMapAttribute>().Any();
if (hasAttribute)
{
classMap.MapMember(propertyInfo);
}
}
}
private void IgnoreMembersWithBsonIgnoreAttribute(BsonClassMap classMap)
{
foreach (var memberMap in classMap.DeclaredMemberMaps.ToList())
{
var ignoreAttribute = (BsonIgnoreAttribute)memberMap.MemberInfo.GetCustomAttributes(inherit: false).OfType<BsonIgnoreAttribute>().FirstOrDefault();
if (ignoreAttribute != null)
{
classMap.UnmapMember(memberMap.MemberInfo);
}
}
}
private void ThrowForDuplicateMemberMapAttributes(BsonClassMap classMap)
{
var nonDuplicatesAlreadySeen = new List<Type>();
foreach (var memberMap in classMap.DeclaredMemberMaps)
{
var attributes = memberMap.MemberInfo.GetCustomAttributes(inherit: false).OfType<IBsonMemberMapAttribute>();
// combine them only if the modifier isn't already in the attributes list...
var attributeTypes = attributes.Select(x => x.GetType());
foreach (var attributeType in attributeTypes)
{
if (nonDuplicatesAlreadySeen.Contains(attributeType))
{
var message = string.Format("Attributes of type {0} can only be applied to a single member.", attributeType);
throw new DuplicateBsonMemberMapAttributeException(message);
}
if (!AllowsDuplicate(attributeType))
{
nonDuplicatesAlreadySeen.Add(attributeType);
}
}
}
}
}
}
}