40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Bson.IO;
|
|
using MongoDB.Bson.Serialization.Conventions;
|
|
|
|
namespace LCU.API.Internal.DocumentLib.Containers;
|
|
|
|
public class DocumentAuditDiscriminatorConvention : IDiscriminatorConvention
|
|
{
|
|
public static string Discriminator = StandardDiscriminatorConvention.Scalar.ElementName;
|
|
public string ElementName => Discriminator;
|
|
|
|
public Type GetActualType(IBsonReader bsonReader, Type nominalType)
|
|
{
|
|
var bookmark = bsonReader.GetBookmark();
|
|
|
|
try
|
|
{
|
|
bsonReader.ReadStartDocument();
|
|
|
|
if (!bsonReader.FindElement(ElementName)) return typeof(DocumentFieldAudit);
|
|
|
|
return bsonReader.ReadString() switch
|
|
{
|
|
nameof(DocumentFieldAudit) => typeof(DocumentFieldAudit),
|
|
nameof(DocumentActionAudit) => typeof(DocumentActionAudit),
|
|
_ => throw new NotSupportedException()
|
|
};
|
|
}
|
|
finally
|
|
{
|
|
bsonReader.ReturnToBookmark(bookmark);
|
|
}
|
|
}
|
|
|
|
public BsonValue GetDiscriminator(Type nominalType, Type actualType)
|
|
{
|
|
return actualType.Name;
|
|
}
|
|
} |