-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DEVEX-222] Added serialization type and merged serialization into Se…
…rialization Context Previously we had DeserializationContext, but after consideration, it'll be better to merge those concepts to make easier customizations per operations (e.g. append, subscribe, etc.).
- Loading branch information
1 parent
cea88dc
commit 4992546
Showing
12 changed files
with
102 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
src/Kurrent.Client/Core/Serialization/DeserializationContext.cs
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/Kurrent.Client/Core/Serialization/SerializationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using EventStore.Client; | ||
|
||
namespace Kurrent.Client.Core.Serialization; | ||
|
||
public record SerializationContext( | ||
SchemaRegistry SchemaRegistry, | ||
SerializationType DefaultSerializationType, | ||
AutomaticDeserialization AutomaticDeserialization | ||
) { | ||
public EventData[] Serialize(IEnumerable<object> messages) { | ||
return Serialize(DefaultSerializationType, messages); | ||
} | ||
|
||
public EventData[] Serialize(SerializationType serializationType, IEnumerable<object> messages) { | ||
if (AutomaticDeserialization == AutomaticDeserialization.Disabled) | ||
throw new InvalidOperationException("Cannot serialize, automatic deserialization is disabled"); | ||
|
||
var serializer = SchemaRegistry.GetSerializer((SchemaDefinitionType)(int)serializationType); | ||
|
||
return messages.Select( | ||
@event => { | ||
var (bytes, typeName) = serializer.Serialize(@event); | ||
return new EventData(Uuid.NewUuid(), typeName, bytes); | ||
} | ||
).ToArray(); | ||
} | ||
|
||
#if NET48 | ||
public bool TryDeserialize(EventRecord eventRecord, out object? deserialized) { | ||
#else | ||
public bool TryDeserialize(EventRecord eventRecord, [NotNullWhen(true)] out object? deserialized) { | ||
#endif | ||
if (AutomaticDeserialization == AutomaticDeserialization.Disabled) { | ||
deserialized = null; | ||
return false; | ||
} | ||
|
||
var schemaDefinitionType = SchemaDefinitionTypeExtensions.FromContentType(eventRecord.ContentType); | ||
|
||
deserialized = SchemaRegistry.GetSerializer(schemaDefinitionType) | ||
.Deserialize(eventRecord.Data, eventRecord.EventType); | ||
|
||
return deserialized != null; | ||
} | ||
|
||
public static SerializationContext From(KurrentClientSerializationSettings? settings = null) { | ||
settings ??= new KurrentClientSerializationSettings(); | ||
|
||
return new SerializationContext( | ||
SchemaRegistry.From(settings), | ||
settings.DefaultSerializationType, | ||
settings.AutomaticDeserialization | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.