public static async Task <EventSourceEvent> ToEvent(this StreamMessage message, CancellationToken cancellationToken = default) { var typeName = message.Type; var version = 0; if (message.TryGetEventMetadata(Constants.VersionKey, out var metaVersion)) { version = Convert.ToInt32(metaVersion, CultureInfo.InvariantCulture); } var eventType = EventCollection.GetEventType(typeName, version); if (eventType == default || eventType == null) { throw new InvalidOperationException($"No Event Type found to deserialize message: {typeName} as version {version}"); } var eventData = await message.GetJsonData(cancellationToken).ConfigureAwait(false); var @event = Serializer.Deserialize(eventData, eventType); if (!(@event is EventSourceEvent eventSourceEvent)) { throw new InvalidOperationException( $"Event Type found to deserialize message: {typeName} at version {version} is not of EventSourceEvent."); } eventSourceEvent.Position = message.Position; return(eventSourceEvent); }
public void ShouldGetCorrectEventType() { // Act var type = EventCollection.GetEventType("Test", 100); // Assert type.ShouldNotBeNull(); type?.FullName.ShouldBe(typeof(TestEventType).FullName); }