Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BsonMapper.cs #1985

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 76 additions & 16 deletions LiteDB/Client/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,69 @@ internal EntityMapper GetEntityMapper(Type type)

return mapper;
}

/// <summary>
/// Override this function to customize the BsonIgnore attribute.
/// </summary>
/// <param name="mi">The member info BsonIgnore attribute will be checked on.</param>
/// <returns>Returns <see langword="true"/> if the specified ignore attribute is defined on this member, otherwise <see langword="false"/>.</returns>
protected virtual bool CheckIgnoreAttribute(MemberInfo mi)
{
// Check if the ignore attribute is set on the given member.
// Use the BsonIgnoreAttribute by default.
return CustomAttributeExtensions.IsDefined(mi, typeof(BsonIgnoreAttribute), true);
}

/// <summary>
/// Override this function to customize BsonField attribute.
/// </summary>
/// <param name="mi">The member info BsonField attribute will be checked on.</param>
/// <returns>Returns the name of the field if the attribute is set. Otherwise <see langword="null"/>.</returns>
protected virtual string CheckFieldAttribute(MemberInfo mi)
{
// Check if the bson field attribute is set on the given member.
// Use the BsonFieldAttribute by default.
BsonFieldAttribute field = (BsonFieldAttribute)CustomAttributeExtensions.GetCustomAttributes(mi, typeof(BsonFieldAttribute), true).FirstOrDefault();
if (field != null && field.Name != null)
return field.Name;

return null;
}

/// <summary>
/// Override this function to customize BsonId attribute.
/// </summary>
/// <param name="mi">The member info BsonId attribute will be checked on.</param>
/// <returns>Returns the value of <seealso cref="BsonIdAttribute.AutoId"/> if set, otherwise <see langword="false"/>.</returns>
protected virtual bool CheckIdAttribute(MemberInfo mi)
{
// Check if the bson id attribute is set on the given member.
// Use the BsonIdAttribute by default.
return ((BsonIdAttribute)CustomAttributeExtensions.GetCustomAttributes(mi, typeof(BsonIdAttribute), true).FirstOrDefault())?.AutoId ?? true;
}

/// <summary>
/// Override this function to customize BsonRef attribute.
/// </summary>
/// <param name="mi">The member info BsonRef attribute will be checked on.</param>
/// <param name="found">Gets whether the attribute was set on <paramref name="mi"/>.</param>
/// <returns>Returns the value of <seealso cref="BsonRefAttribute.Collection"/> if set, otherwise <see langword="null"/>.</returns>
protected virtual string CheckRefAttribute(MemberInfo mi, out bool found)
{
// Check if the bson ref attribute is set on the given member.
// Use the BsonRefAttribute by default.

// TODO: Consider wrapping the return value to a specific interface instead of requiring 'out'.
BsonRefAttribute attr =((BsonRefAttribute)CustomAttributeExtensions.GetCustomAttributes(mi, typeof(BsonRefAttribute), false).FirstOrDefault());
if (attr == null)
{
found = false;
return null;
}

found = true;
return attr.Collection;
}

/// <summary>
/// Use this method to override how your class can be, by default, mapped from entity to Bson document.
Expand All @@ -253,20 +316,17 @@ protected virtual EntityMapper BuildEntityMapper(Type type)

foreach (var memberInfo in members)
{
// checks [BsonIgnore]
if (CustomAttributeExtensions.IsDefined(memberInfo, ignoreAttr, true)) continue;
// Check [BsonIgnore].
if (CheckIgnoreAttribute(memberInfo)) continue;

// checks field name conversion
var name = this.ResolveFieldName(memberInfo.Name);

// check if property has [BsonField]
var field = (BsonFieldAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, fieldAttr, true).FirstOrDefault();

string fn = CheckFieldAttribute(memberInfo);
// check if property has [BsonField] with a custom field name
if (field != null && field.Name != null)
{
name = field.Name;
}
if (fn != null) name = fn;

// checks if memberInfo is id field
if (memberInfo == id)
Expand All @@ -279,7 +339,7 @@ protected virtual EntityMapper BuildEntityMapper(Type type)
var setter = Reflection.CreateGenericSetter(type, memberInfo);

// check if property has [BsonId] to get with was setted AutoId = true
var autoId = (BsonIdAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, idAttr, true).FirstOrDefault();
bool autoId = CheckIdAttribute(memberInfo);

// get data type
var dataType = memberInfo is PropertyInfo ?
Expand All @@ -292,7 +352,7 @@ protected virtual EntityMapper BuildEntityMapper(Type type)
// create a property mapper
var member = new MemberMapper
{
AutoId = autoId == null ? true : autoId.AutoId,
AutoId = autoId,
FieldName = name,
MemberName = memberInfo.Name,
DataType = dataType,
Expand All @@ -302,12 +362,12 @@ protected virtual EntityMapper BuildEntityMapper(Type type)
Setter = setter
};

// check if property has [BsonRef]
var dbRef = (BsonRefAttribute)CustomAttributeExtensions.GetCustomAttributes(memberInfo, dbrefAttr, false).FirstOrDefault();
// Check if property has [BsonRef].
string dbRef = CheckRefAttribute(memberInfo, out bool dbRefFound);

if (dbRef != null && memberInfo is PropertyInfo)
if (dbRefFound && memberInfo is PropertyInfo)
{
BsonMapper.RegisterDbRef(this, member, _typeNameBinder, dbRef.Collection ?? this.ResolveCollectionName((memberInfo as PropertyInfo).PropertyType));
BsonMapper.RegisterDbRef(this, member, _typeNameBinder, dbRef ?? this.ResolveCollectionName((memberInfo as PropertyInfo).PropertyType));
}

// support callback to user modify member mapper
Expand All @@ -322,7 +382,7 @@ protected virtual EntityMapper BuildEntityMapper(Type type)

return mapper;
}

/// <summary>
/// Gets MemberInfo that refers to Id from a document object.
/// </summary>
Expand Down Expand Up @@ -620,4 +680,4 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT

#endregion
}
}
}