Skip to content

Commit

Permalink
Reverts public changes to IAsyncParseNodeFactory to avoid breaking ba…
Browse files Browse the repository at this point in the history
…ckward compatibility
  • Loading branch information
MihaMarkic committed Apr 24, 2024
1 parent 7d5acb2 commit c89c76f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/ApiClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ApiClientBuilder
/// Registers the default deserializer to the registry.
/// </summary>
/// <typeparam name="T">The type of the parse node factory to register</typeparam>
public static void RegisterDefaultDeserializer<T>() where T : IAsyncParseNodeFactory, new()
public static void RegisterDefaultDeserializer<T>() where T : IParseNodeFactory, new()
{
var deserializerFactory = new T();
ParseNodeFactoryRegistry.DefaultInstance
Expand Down Expand Up @@ -60,9 +60,9 @@ public static ISerializationWriterFactory EnableBackingStoreForSerializationWrit
/// </summary>
/// <param name="original">The parse node factory to enable the backing store on.</param>
/// <returns>A new parse node factory with the backing store enabled.</returns>
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);
Expand Down
19 changes: 16 additions & 3 deletions src/serialization/ParseNodeFactoryRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public string ValidContentType
/// <summary>
/// List of factories that are registered by content type.
/// </summary>
public ConcurrentDictionary<string, IAsyncParseNodeFactory> ContentTypeAssociatedFactories { get; set; } = new();
public ConcurrentDictionary<string, IParseNodeFactory> ContentTypeAssociatedFactories { get; set; } = new();
internal static readonly Regex contentTypeVendorCleanupRegex = new(@"[^/]+\+", RegexOptions.Compiled);

/// <summary>
Expand Down Expand Up @@ -76,11 +76,24 @@ public async Task<IParseNode> 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");
}
Expand Down
10 changes: 7 additions & 3 deletions src/serialization/ParseNodeProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class ParseNodeProxyFactory : IAsyncParseNodeFactory
/// The valid content type for the <see cref="ParseNodeProxyFactory"/> instance
/// </summary>
public string ValidContentType { get { return _concrete.ValidContentType; } }
private readonly IAsyncParseNodeFactory _concrete;
private readonly IParseNodeFactory _concrete;
private readonly Action<IParsable> _onBefore;
private readonly Action<IParsable> _onAfter;
/// <summary>
Expand All @@ -27,7 +27,7 @@ public abstract class ParseNodeProxyFactory : IAsyncParseNodeFactory
/// <param name="concrete">The concrete factory to wrap.</param>
/// <param name="onBefore">The callback to invoke before the deserialization of any model object.</param>
/// <param name="onAfter">The callback to invoke after the deserialization of any model object.</param>
public ParseNodeProxyFactory(IAsyncParseNodeFactory concrete, Action<IParsable> onBefore, Action<IParsable> onAfter)
public ParseNodeProxyFactory(IParseNodeFactory concrete, Action<IParsable> onBefore, Action<IParsable> onAfter)
{
_concrete = concrete ?? throw new ArgumentNullException(nameof(concrete));
_onBefore = onBefore;
Expand Down Expand Up @@ -67,7 +67,11 @@ public IParseNode GetRootParseNode(string contentType, Stream content)
public async Task<IParseNode> 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) =>
Expand Down
2 changes: 1 addition & 1 deletion src/store/BackingStoreParseNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BackingStoreParseNodeFactory : ParseNodeProxyFactory
/// <summary>
/// Initializes a new instance of the <see cref="BackingStoreParseNodeFactory"/> class given a concrete implementation of <see cref="IParseNodeFactory"/>.
/// </summary>
public BackingStoreParseNodeFactory(IAsyncParseNodeFactory concrete) : base(
public BackingStoreParseNodeFactory(IParseNodeFactory concrete) : base(
concrete,
(x) =>
{
Expand Down

0 comments on commit c89c76f

Please sign in to comment.