From c89c76f4dc56b9501b42b8683263b82a82011171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Wed, 24 Apr 2024 09:04:29 +0200 Subject: [PATCH] Reverts public changes to IAsyncParseNodeFactory to avoid breaking backward compatibility --- src/ApiClientBuilder.cs | 6 +++--- src/serialization/ParseNodeFactoryRegistry.cs | 19 ++++++++++++++++--- src/serialization/ParseNodeProxyFactory.cs | 10 +++++++--- src/store/BackingStoreParseNodeFactory.cs | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/ApiClientBuilder.cs b/src/ApiClientBuilder.cs index 70bf3445..141be487 100644 --- a/src/ApiClientBuilder.cs +++ b/src/ApiClientBuilder.cs @@ -29,7 +29,7 @@ public static class ApiClientBuilder /// Registers the default deserializer to the registry. /// /// The type of the parse node factory to register - public static void RegisterDefaultDeserializer() where T : IAsyncParseNodeFactory, new() + public static void RegisterDefaultDeserializer() where T : IParseNodeFactory, new() { var deserializerFactory = new T(); ParseNodeFactoryRegistry.DefaultInstance @@ -60,9 +60,9 @@ public static ISerializationWriterFactory EnableBackingStoreForSerializationWrit /// /// The parse node factory to enable the backing store on. /// A new parse node factory with the backing store enabled. - public static IAsyncParseNodeFactory EnableBackingStoreForParseNodeFactory(IAsyncParseNodeFactory original) + public static IParseNodeFactory EnableBackingStoreForParseNodeFactory(IParseNodeFactory original) { - IAsyncParseNodeFactory result = original ?? throw new ArgumentNullException(nameof(original)); + var result = original ?? throw new ArgumentNullException(nameof(original)); if(original is ParseNodeFactoryRegistry registry) { EnableBackingStoreForParseNodeRegistry(registry); diff --git a/src/serialization/ParseNodeFactoryRegistry.cs b/src/serialization/ParseNodeFactoryRegistry.cs index 8806a90b..adb8c837 100644 --- a/src/serialization/ParseNodeFactoryRegistry.cs +++ b/src/serialization/ParseNodeFactoryRegistry.cs @@ -34,7 +34,7 @@ public string ValidContentType /// /// List of factories that are registered by content type. /// - public ConcurrentDictionary ContentTypeAssociatedFactories { get; set; } = new(); + public ConcurrentDictionary ContentTypeAssociatedFactories { get; set; } = new(); internal static readonly Regex contentTypeVendorCleanupRegex = new(@"[^/]+\+", RegexOptions.Compiled); /// @@ -76,11 +76,24 @@ public async Task GetRootParseNodeAsync(string contentType, Stream c var vendorSpecificContentType = contentType.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).First(); if(ContentTypeAssociatedFactories.TryGetValue(vendorSpecificContentType, out var vendorFactory)) - return await vendorFactory.GetRootParseNodeAsync(vendorSpecificContentType, content, cancellationToken).ConfigureAwait(false); + { + if(vendorFactory is not IAsyncParseNodeFactory vendorFactoryAsync) + { + throw new Exception("IAsyncParseNodeFactory is required for async operations"); + } + + return await vendorFactoryAsync.GetRootParseNodeAsync(vendorSpecificContentType, content, cancellationToken).ConfigureAwait(false); + } var cleanedContentType = contentTypeVendorCleanupRegex.Replace(vendorSpecificContentType, string.Empty); if(ContentTypeAssociatedFactories.TryGetValue(cleanedContentType, out var factory)) - return await factory.GetRootParseNodeAsync(cleanedContentType, content, cancellationToken); + { + if(factory is not IAsyncParseNodeFactory vendorFactoryAsync) + { + throw new Exception("IAsyncParseNodeFactory is required for async operations"); + } + return await vendorFactoryAsync.GetRootParseNodeAsync(cleanedContentType, content, cancellationToken); + } throw new InvalidOperationException($"Content type {cleanedContentType} does not have a factory registered to be parsed"); } diff --git a/src/serialization/ParseNodeProxyFactory.cs b/src/serialization/ParseNodeProxyFactory.cs index 749f3a71..8d08380a 100644 --- a/src/serialization/ParseNodeProxyFactory.cs +++ b/src/serialization/ParseNodeProxyFactory.cs @@ -18,7 +18,7 @@ public abstract class ParseNodeProxyFactory : IAsyncParseNodeFactory /// The valid content type for the instance /// public string ValidContentType { get { return _concrete.ValidContentType; } } - private readonly IAsyncParseNodeFactory _concrete; + private readonly IParseNodeFactory _concrete; private readonly Action _onBefore; private readonly Action _onAfter; /// @@ -27,7 +27,7 @@ public abstract class ParseNodeProxyFactory : IAsyncParseNodeFactory /// The concrete factory to wrap. /// The callback to invoke before the deserialization of any model object. /// The callback to invoke after the deserialization of any model object. - public ParseNodeProxyFactory(IAsyncParseNodeFactory concrete, Action onBefore, Action onAfter) + public ParseNodeProxyFactory(IParseNodeFactory concrete, Action onBefore, Action onAfter) { _concrete = concrete ?? throw new ArgumentNullException(nameof(concrete)); _onBefore = onBefore; @@ -67,7 +67,11 @@ public IParseNode GetRootParseNode(string contentType, Stream content) public async Task GetRootParseNodeAsync(string contentType, Stream content, CancellationToken cancellationToken = default) { - var node = await _concrete.GetRootParseNodeAsync(contentType, content).ConfigureAwait(false); + if (_concrete is not IAsyncParseNodeFactory asyncConcrete) + { + throw new Exception("IAsyncParseNodeFactory is required for async operations"); + } + var node = await asyncConcrete.GetRootParseNodeAsync(contentType, content).ConfigureAwait(false); var originalBefore = node.OnBeforeAssignFieldValues; var originalAfter = node.OnAfterAssignFieldValues; node.OnBeforeAssignFieldValues = (x) => diff --git a/src/store/BackingStoreParseNodeFactory.cs b/src/store/BackingStoreParseNodeFactory.cs index d555a682..965bfbdc 100644 --- a/src/store/BackingStoreParseNodeFactory.cs +++ b/src/store/BackingStoreParseNodeFactory.cs @@ -14,7 +14,7 @@ public class BackingStoreParseNodeFactory : ParseNodeProxyFactory /// /// Initializes a new instance of the class given a concrete implementation of . /// - public BackingStoreParseNodeFactory(IAsyncParseNodeFactory concrete) : base( + public BackingStoreParseNodeFactory(IParseNodeFactory concrete) : base( concrete, (x) => {