-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Cratis.Applications.MongoDB; | ||
using Cratis.Chronicle.Concepts.Jobs; | ||
using Cratis.Chronicle.Storage.Jobs; | ||
using Cratis.Strings; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
|
@@ -15,21 +17,67 @@ namespace Cratis.Chronicle.Storage.MongoDB.Jobs; | |
/// <param name="jobTypes">The <see cref="IJobTypes"/>.</param> | ||
public class JobStateSerializer(IJobTypes jobTypes) : SerializerBase<JobState> | ||
{ | ||
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, JobState value) | ||
{ | ||
var requestType = value.Request.GetType(); | ||
var requestSerializer = BsonSerializer.SerializerRegistry.GetSerializer(requestType); | ||
} | ||
/// <inheritdoc /> | ||
public override JobState Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) | ||
{ | ||
var jobRequestElementName = nameof(JobState.Request).ToCamelCase(); | ||
var rawBsonDocument = context.Reader.ReadRawBsonDocument(); | ||
using var rawDocument = new RawBsonDocument(rawBsonDocument); | ||
rawDocument.Remove(jobRequestElementName); | ||
var bsonDocument = rawDocument.ToBsonDocument<BsonDocument>(); | ||
var jobTypeString = bsonDocument.GetValue(nameof(JobState.Type).ToCamelCase()).AsString; | ||
var jobRequestType = jobTypes.GetRequestClrTypeFor(new(jobTypeString)).AsT0; | ||
var jobRequestElementName = nameof(JobState.Request).ToCamelCase(); | ||
var request = (IJobRequest)BsonSerializer.Deserialize( | ||
bsonDocument.GetElement(jobRequestElementName).ToBsonDocument(), | ||
jobRequestType); | ||
bsonDocument.Remove(jobRequestElementName); | ||
// bsonDocument.Remove(jobRequestElementName); | ||
Check warning on line 38 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 38 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 38 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
|
||
var jobState = BsonSerializer.Deserialize<JobState>(bsonDocument); | ||
jobState.Request = request; | ||
return jobState; | ||
} | ||
} | ||
} | ||
|
||
public class JobRequestSerializer<TJobRequest>(IJobTypes jobTypes) : SerializerBase<TJobRequest> | ||
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
Check warning on line 45 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
|
||
where TJobRequest : IJobRequest | ||
{ | ||
/// <inheritdoc /> | ||
public override TJobRequest Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) | ||
{ | ||
var rawBsonDocument = context.Reader.ReadRawBsonDocument(); | ||
using var rawDocument = new RawBsonDocument(rawBsonDocument); | ||
var bsonDocument = rawDocument.ToBsonDocument<BsonDocument>(); | ||
return BsonSerializer.Deserialize<TJobRequest>(bsonDocument); | ||
} | ||
} | ||
|
||
/// <summary> | ||
Check warning on line 58 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 58 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
|
||
/// Represents a <see cref="IBsonSerializationProvider"/> for concepts. | ||
/// </summary> | ||
public class JobRequestSerializationProvider(IServiceProvider serviceProvider) : IBsonSerializationProvider | ||
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / publish-nuget-packages
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / kernel
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
Check warning on line 61 in Source/Kernel/Storage.MongoDB/Jobs/JobStateSerializer.cs GitHub Actions / integration
|
||
{ | ||
/// <summary> | ||
/// Creates an instance of a serializer of the concept of the given type param T. | ||
/// </summary> | ||
/// <typeparam name="T">The Concept type.</typeparam> | ||
/// <returns><see cref="ConceptSerializer{T}"/> for the specific type.</returns> | ||
public static JobRequestSerializer<T> CreateSerializer<T>(IServiceProvider serviceProvider) | ||
where T : class, IJobRequest | ||
=> ActivatorUtilities.CreateInstance<JobRequestSerializer<T>>(serviceProvider); | ||
|
||
/// <inheritdoc/> | ||
public IBsonSerializer GetSerializer(Type type) | ||
{ | ||
if (type.IsAssignableTo(typeof(IJobRequest))) | ||
{ | ||
var createSerializerMethod = GetType().GetMethod(nameof(CreateSerializer))!.MakeGenericMethod(type); | ||
return (createSerializerMethod.Invoke(null, [serviceProvider]) as IBsonSerializer)!; | ||
} | ||
|
||
return null!; | ||
} | ||
} |