Skip to content

Commit 6fa21a5

Browse files
committed
Simplified ISerializer
1 parent 20cdbf2 commit 6fa21a5

File tree

22 files changed

+156
-221
lines changed

22 files changed

+156
-221
lines changed

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RegistrationTests/RActionWithStateRegistrationTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,13 @@ private class Serializer : ISerializer
4747
public bool Invoked { get; set; }
4848
private ISerializer Default { get; } = DefaultSerializer.Instance;
4949

50-
public byte[] Serialize(object? value, Type type)
50+
public byte[] Serialize(object value, Type type)
5151
{
5252
Invoked = true;
5353
return Default.Serialize(value, type);
5454
}
5555

56-
public void Serialize(object value, out byte[] valueBytes, out byte[] typeBytes)
57-
=> Default.Serialize(value, out valueBytes, out typeBytes);
58-
5956
public object Deserialize(byte[] json, Type type)
6057
=> Default.Deserialize(json, type);
61-
62-
public StoredException SerializeException(FatalWorkflowException exception)
63-
=> Default.SerializeException(exception);
64-
public FatalWorkflowException DeserializeException(FlowId flowId, StoredException storedException)
65-
=> Default.DeserializeException(flowId, storedException);
66-
67-
public object DeserializeMessage(byte[] json, byte[] type)
68-
=> Default.DeserializeMessage(json, type);
6958
}
7059
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RegistrationTests/RFuncRegistrationTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,13 @@ private class Serializer : ISerializer
5353
public bool Invoked { get; set; }
5454
private ISerializer Default { get; } = DefaultSerializer.Instance;
5555

56-
public byte[] Serialize(object? value, Type type)
56+
public byte[] Serialize(object value, Type type)
5757
{
5858
Invoked = true;
5959
return Default.Serialize(value, type);
6060
}
6161

62-
public void Serialize(object value, out byte[] valueBytes, out byte[] typeBytes)
63-
=> Default.Serialize(value, out valueBytes, out typeBytes);
64-
6562
public object Deserialize(byte[] json, Type type)
6663
=> Default.Deserialize(json, type);
67-
68-
public StoredException SerializeException(FatalWorkflowException exception)
69-
=> Default.SerializeException(exception);
70-
public FatalWorkflowException DeserializeException(FlowId flowId, StoredException storedException)
71-
=> Default.DeserializeException(flowId, storedException);
72-
73-
public object DeserializeMessage(byte[] json, byte[] type)
74-
=> Default.DeserializeMessage(json, type);
7564
}
7665
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RegistrationTests/RFuncWithStateRegistrationTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,13 @@ private class Serializer : ISerializer
5757
public bool Invoked { get; set; }
5858
private ISerializer Default { get; } = DefaultSerializer.Instance;
5959

60-
public byte[] Serialize(object? value, Type type)
60+
public byte[] Serialize(object value, Type type)
6161
{
6262
Invoked = true;
6363
return Default.Serialize(value, type);
6464
}
6565

66-
public void Serialize(object value, out byte[] valueBytes, out byte[] typeBytes)
67-
=> Default.Serialize(value, out valueBytes, out typeBytes);
68-
6966
public object Deserialize(byte[] bytes, Type type)
7067
=> Default.Deserialize(bytes, type);
71-
72-
public StoredException SerializeException(FatalWorkflowException exception)
73-
=> Default.SerializeException(exception);
74-
public FatalWorkflowException DeserializeException(FlowId flowId, StoredException storedException)
75-
=> Default.DeserializeException(flowId, storedException);
76-
77-
public object DeserializeMessage(byte[] json, byte[] type)
78-
=> Default.DeserializeMessage(json, type);
7968
}
8069
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/SerializationTests.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using Cleipnir.ResilientFunctions.CoreRuntime.Serialization;
33
using Cleipnir.ResilientFunctions.Domain;
4+
using Cleipnir.ResilientFunctions.Helpers;
5+
using Cleipnir.ResilientFunctions.Storage;
46
using Cleipnir.ResilientFunctions.Tests.Utils;
57
using Microsoft.VisualStudio.TestTools.UnitTesting;
68
using Shouldly;
@@ -15,9 +17,10 @@ public void ConcreteTypeOfEventIsSerializedAndDeserializedByDefaultSerializer()
1517
{
1618
var serializer = DefaultSerializer.Instance;
1719
Parent @event = new Child("Hello World");
18-
serializer.Serialize(@event, out var content, out var type);
20+
var content = serializer.Serialize(@event, @event.GetType());
21+
var type = serializer.SerializeType(@event.GetType());
1922
var serialized = new SerializedMessage(content, type);
20-
var deserialized = serializer.DeserializeMessage(serialized.Content, serialized.Type);
23+
var deserialized = serializer.Deserialize(serialized.Content, serializer.ResolveType(serialized.Type)!);
2124
if (deserialized is not Child child)
2225
throw new Exception("Expected event to be of child-type");
2326

@@ -52,8 +55,8 @@ public void ExceptionCanBeConvertedToAndFromFatalWorkflowException()
5255
fatalWorkflowException.FlowErrorMessage.ShouldBe("Something went wrong");
5356
fatalWorkflowException.FlowStackTrace.ShouldNotBeNull();
5457

55-
var storedException = serializer.SerializeException(fatalWorkflowException);
56-
var deserializedException = serializer.DeserializeException(flowId, storedException);
58+
var storedException = fatalWorkflowException.ToStoredException();
59+
var deserializedException = FatalWorkflowException.Create(flowId, storedException);
5760

5861
(deserializedException is FatalWorkflowException<InvalidOperationException>).ShouldBeTrue();
5962
deserializedException.ErrorType.ShouldBe(typeof(InvalidOperationException));
@@ -63,4 +66,30 @@ public void ExceptionCanBeConvertedToAndFromFatalWorkflowException()
6366

6467
public record Parent;
6568
public record Child(string Value) : Parent;
69+
70+
[TestMethod]
71+
public void ImplementingClassCanOverrideResolveTypeDefaultMethod()
72+
{
73+
ISerializer defaultSerializer = DefaultSerializer.Instance;
74+
ISerializer customSerializer = new CustomResolveTypeSerializer();
75+
76+
// Default implementation uses Type.GetType
77+
defaultSerializer.ResolveType(typeof(string).SimpleQualifiedName().ToUtf8Bytes()).ShouldBe(typeof(string));
78+
79+
// Custom implementation always returns typeof(int) regardless of input
80+
customSerializer.ResolveType(typeof(string).SimpleQualifiedName().ToUtf8Bytes()).ShouldBe(typeof(int));
81+
customSerializer.ResolveType("anything".ToUtf8Bytes()).ShouldBe(typeof(int));
82+
}
83+
84+
private class CustomResolveTypeSerializer : ISerializer
85+
{
86+
public byte[] Serialize(object value, Type type)
87+
=> throw new NotImplementedException();
88+
89+
public object Deserialize(byte[] bytes, Type type)
90+
=> throw new NotImplementedException();
91+
92+
// Override the default interface method
93+
public Type? ResolveType(byte[] type) => typeof(int);
94+
}
6695
}

Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/CustomMessageSerializerTests.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,17 @@ private class EventSerializer : ISerializer
4444
public static Utils.SyncedList<object> EventToSerialize { get; } = new();
4545
public static Utils.SyncedList<Tuple<string, string>> EventToDeserialize { get; }= new();
4646

47-
public byte[] Serialize(object? value, Type type) => DefaultSerializer.Instance.Serialize(value, type);
48-
49-
public void Serialize(object value, out byte[] valueBytes, out byte[] typeBytes)
47+
public byte[] Serialize(object value, Type type)
5048
{
5149
EventToSerialize.Add(value);
52-
DefaultSerializer.Instance.Serialize(value, out valueBytes, out typeBytes);
50+
return DefaultSerializer.Instance.Serialize(value, type);
5351
}
5452

5553
public object Deserialize(byte[] json, Type type)
56-
=> DefaultSerializer.Instance.Deserialize(json, type);
57-
58-
public StoredException SerializeException(FatalWorkflowException exception)
59-
=> DefaultSerializer.Instance.SerializeException(exception);
60-
public FatalWorkflowException DeserializeException(FlowId flowId, StoredException storedException)
61-
=> DefaultSerializer.Instance.DeserializeException(flowId, storedException);
62-
63-
public object DeserializeMessage(byte[] json, byte[] type)
6454
{
65-
EventToDeserialize.Add(Tuple.Create(json.ToStringFromUtf8Bytes(), type.ToStringFromUtf8Bytes()));
66-
return DefaultSerializer.Instance.DeserializeMessage(json, type);
55+
EventToDeserialize.Add(Tuple.Create(json.ToStringFromUtf8Bytes(), type.FullName!));
56+
return DefaultSerializer.Instance.Deserialize(json, type);
6757
}
58+
6859
}
6960
}

Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/MessageStoreTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ await functionStore.CreateFunction(
506506
var newEvents = await messageStore.GetMessages(functionId, skip);
507507
newEvents.Count.ShouldBe(1);
508508
var storedEvent = newEvents[0];
509-
var @event = DefaultSerializer.Instance.DeserializeMessage(storedEvent.MessageContent, storedEvent.MessageType);
509+
var @event = DefaultSerializer.Instance.Deserialize(storedEvent.MessageContent, DefaultSerializer.Instance.ResolveType(storedEvent.MessageType)!);
510510
@event.ShouldBe("hello world");
511511
storedEvent.IdempotencyKey.ShouldBe("idempotency_key_1");
512512
skip = storedEvent.Position + 1;
@@ -522,7 +522,7 @@ await functionStore.CreateFunction(
522522
newEvents = await messageStore.GetMessages(functionId, skip);
523523
newEvents.Count.ShouldBe(1);
524524
storedEvent = newEvents[0];
525-
@event = DefaultSerializer.Instance.DeserializeMessage(storedEvent.MessageContent, storedEvent.MessageType);
525+
@event = DefaultSerializer.Instance.Deserialize(storedEvent.MessageContent, DefaultSerializer.Instance.ResolveType(storedEvent.MessageType)!);
526526
@event.ShouldBe("hello universe");
527527
storedEvent.IdempotencyKey.ShouldBe("idempotency_key_2");
528528
skip = storedEvent.Position + 1;
@@ -559,7 +559,7 @@ await functionStore.CreateFunction(
559559
var newEvents = await messageStore.GetMessages(functionId, skip);
560560
newEvents.Count.ShouldBe(1);
561561
var storedEvent = newEvents[0];
562-
var @event = DefaultSerializer.Instance.DeserializeMessage(storedEvent.MessageContent, storedEvent.MessageType);
562+
var @event = DefaultSerializer.Instance.Deserialize(storedEvent.MessageContent, DefaultSerializer.Instance.ResolveType(storedEvent.MessageType)!);
563563
@event.ShouldBe("hello world");
564564
storedEvent.IdempotencyKey.ShouldBe("idempotency_key_1");
565565
skip = storedEvent.Position + 1;

Core/Cleipnir.ResilientFunctions.Tests/Messaging/TestTemplates/MessagesSubscriptionTests.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ await messageStore.AppendMessage(
5252
events.Count.ShouldBe(1);
5353
DefaultSerializer
5454
.Instance
55-
.DeserializeMessage(events[0].MessageContent, events[0].MessageType)
55+
.Deserialize(events[0].MessageContent, DefaultSerializer.Instance.ResolveType(events[0].MessageType)!)
5656
.ShouldBe("hello world");
5757

5858
var skipPosition = events[0].Position + 1;
@@ -69,7 +69,7 @@ await messageStore.AppendMessage(
6969

7070
DefaultSerializer
7171
.Instance
72-
.DeserializeMessage(events[0].MessageContent, events[0].MessageType)
72+
.Deserialize(events[0].MessageContent, DefaultSerializer.Instance.ResolveType(events[0].MessageType)!)
7373
.ShouldBe("hello universe");
7474
}
7575

@@ -942,27 +942,16 @@ private class ExceptionThrowingEventSerializer : ISerializer
942942
public ExceptionThrowingEventSerializer(Type failDeserializationOnType)
943943
=> _failDeserializationOnType = failDeserializationOnType;
944944

945-
public byte[] Serialize(object? value, Type type) => DefaultSerializer.Instance.Serialize(value, type);
946-
947-
public void Serialize(object value, out byte[] valueBytes, out byte[] typeBytes)
948-
=> DefaultSerializer.Instance.Serialize(value, out valueBytes, out typeBytes);
945+
public byte[] Serialize(object value, Type type)
946+
=> DefaultSerializer.Instance.Serialize(value, type);
949947

950948
public object Deserialize(byte[] json, Type type)
951-
=> DefaultSerializer.Instance.Deserialize(json, type);
952-
953-
public StoredException SerializeException(FatalWorkflowException exception)
954-
=> DefaultSerializer.Instance.SerializeException(exception);
955-
956-
public FatalWorkflowException DeserializeException(FlowId flowId, StoredException storedException)
957-
=> DefaultSerializer.Instance.DeserializeException(flowId, storedException);
958-
959-
public object DeserializeMessage(byte[] json, byte[] type)
960949
{
961-
var eventType = Type.GetType(type.ToStringFromUtf8Bytes())!;
962-
if (eventType == _failDeserializationOnType)
950+
if (type == _failDeserializationOnType)
963951
throw new DeserializationException("Deserialization failed for BadMessage", new Exception("Inner cause"));
964952

965-
return DefaultSerializer.Instance.DeserializeMessage(json, type);
953+
return DefaultSerializer.Instance.Deserialize(json, type);
966954
}
955+
967956
}
968957
}

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/FunctionTests/EffectTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,11 @@ public async Task ExistingEffectsFuncIsOnlyInvokedAfterGettingValue(Task<IFuncti
361361

362362
// Create an existing effect
363363
var effectId = new EffectId([1]);
364+
var serializedResult = DefaultSerializer.Instance.Serialize(42, typeof(int));
364365
var existingEffect = new StoredEffect(
365366
effectId,
366367
WorkStatus.Completed,
367-
Result: DefaultSerializer.Instance.Serialize(42),
368+
Result: serializedResult,
368369
StoredException: null,
369370
Alias: "test_alias"
370371
);

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/StoreTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ await store.FailFunction(
491491
sf.ShouldNotBeNull();
492492
sf.Status.ShouldBe(Status.Failed);
493493
sf.Exception.ShouldNotBeNull();
494-
var fatalWorkflowException = DefaultSerializer.Instance.DeserializeException(flowId, sf.Exception);
494+
var fatalWorkflowException = FatalWorkflowException.Create(flowId, sf.Exception);
495495
fatalWorkflowException.FlowErrorMessage.ShouldBe(storedException.ExceptionMessage);
496496
fatalWorkflowException.FlowStackTrace.ShouldBe(storedException.ExceptionStackTrace);
497497
fatalWorkflowException.ErrorType.ShouldBe(typeof(Exception));
@@ -581,7 +581,7 @@ await store.SetFunctionState(
581581

582582
var storedMessages = await store.MessageStore.GetMessages(functionId, skip: 0);
583583
storedMessages.Count.ShouldBe(1);
584-
var deserializedMessage = (string) DefaultSerializer.Instance.DeserializeMessage(storedMessages[0].MessageContent, storedMessages[0].MessageType);
584+
var deserializedMessage = (string) DefaultSerializer.Instance.Deserialize(storedMessages[0].MessageContent, DefaultSerializer.Instance.ResolveType(storedMessages[0].MessageType)!);
585585
deserializedMessage.ShouldBe("hello everyone");
586586
}
587587

0 commit comments

Comments
 (0)