Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/ByteBard.AsyncAPI.Readers/BindingDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using System.Collections.Generic;
using System.Linq;
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Interface;
using ByteBard.AsyncAPI.Readers.ParseNodes;

public class BindingDeserializer
Expand All @@ -17,6 +22,52 @@ public static T LoadBinding<T>(string nodeName, ParseNode node, FixedFieldMap<T>
return binding;
}

internal static AsyncApiBindings<TBinding> LoadBindings<TBinding>(
MapNode mapNode,
IDictionary<string, IBindingParser<TBinding>> parsers,
PatternFieldMap<AsyncApiBindings<TBinding>> patternFields)
where TBinding : IBinding
{
var context = mapNode.Context;
var bindings = new AsyncApiBindings<TBinding>();
foreach (var property in mapNode)
{
if (patternFields.Keys.Any(isMatch => isMatch(property.Name)))
{
continue;
}

if (!parsers.TryGetValue(property.Name, out var parser))
{
// TBinding is the binding interface (e.g. IChannelBinding), so drop the leading "I" for the diagnostic.
var bindingType = typeof(TBinding).Name.Substring(1);

// Reported as an error because the binding is dropped from the model and cannot be written back.
context.Diagnostic.Errors.Add(
new AsyncApiError(context.GetLocation(), $"{bindingType} {property.Name} is not found"));
continue;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

try
{
var binding = parser.LoadBinding(property);
if (binding != null)
{
bindings.Add(binding);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = context.GetLocation();
context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}
}

mapNode.ParseFields(bindings, patternFields);

return bindings;
}

private static PatternFieldMap<T> BindingPatternExtensionFields<T>()
where T : IBinding, new()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;

internal static partial class AsyncApiV2Deserializer
{
private static readonly PatternFieldMap<AsyncApiBindings<IChannelBinding>> channelBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("channelBindings");
Expand All @@ -16,42 +22,7 @@ internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode
return new AsyncApiBindingsReference<IChannelBinding>(pointer);
}

var channelBindings = new AsyncApiBindings<IChannelBinding>();
foreach (var property in mapNode)
{
var channelBinding = LoadChannelBinding(property);

if (channelBinding != null)
{
channelBindings.Add(channelBinding);
}
else
{
mapNode.Context.Diagnostic.Errors.Add(
new AsyncApiError(node.Context.GetLocation(), $"ChannelBinding {property.Name} is not found"));
}
}

return channelBindings;
}

private static IChannelBinding LoadChannelBinding(ParseNode node)
{
var property = node as PropertyNode;
try
{
if (node.Context.ChannelBindingParsers.TryGetValue(property.Name, out var parser))
{
return parser.LoadBinding(property);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = node.Context.GetLocation();
node.Context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}

return null;
return BindingDeserializer.LoadBindings(mapNode, node.Context.ChannelBindingParsers, channelBindingPatternFields);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;

internal static partial class AsyncApiV2Deserializer
{
private static readonly PatternFieldMap<AsyncApiBindings<IMessageBinding>> messageBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

internal static AsyncApiBindings<IMessageBinding> LoadMessageBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("messageBindings");
Expand All @@ -16,42 +22,7 @@ internal static AsyncApiBindings<IMessageBinding> LoadMessageBindings(ParseNode
return new AsyncApiBindingsReference<IMessageBinding>(pointer);
}

var messageBindings = new AsyncApiBindings<IMessageBinding>();
foreach (var property in mapNode)
{
var messageBinding = LoadMessageBinding(property);

if (messageBinding != null)
{
messageBindings.Add(messageBinding);
}
else
{
mapNode.Context.Diagnostic.Errors.Add(
new AsyncApiError(node.Context.GetLocation(), $"MessageBinding {property.Name} is not found"));
}
}

return messageBindings;
}

internal static IMessageBinding LoadMessageBinding(ParseNode node)
{
var property = node as PropertyNode;
try
{
if (node.Context.MessageBindingParsers.TryGetValue(property.Name, out var parser))
{
return parser.LoadBinding(property);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = node.Context.GetLocation();
node.Context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}

return null;
return BindingDeserializer.LoadBindings(mapNode, node.Context.MessageBindingParsers, messageBindingPatternFields);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;

internal static partial class AsyncApiV2Deserializer
{
private static readonly PatternFieldMap<AsyncApiBindings<IOperationBinding>> operationBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

internal static AsyncApiBindings<IOperationBinding> LoadOperationBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("operationBindings");
Expand All @@ -16,42 +22,7 @@ internal static AsyncApiBindings<IOperationBinding> LoadOperationBindings(ParseN
return new AsyncApiBindingsReference<IOperationBinding>(pointer);
}

var operationBindings = new AsyncApiBindings<IOperationBinding>();
foreach (var property in mapNode)
{
var operationBinding = LoadOperationBinding(property);

if (operationBinding != null)
{
operationBindings.Add(operationBinding);
}
else
{
mapNode.Context.Diagnostic.Errors.Add(
new AsyncApiError(node.Context.GetLocation(), $"OperationBinding {property.Name} is not found"));
}
}

return operationBindings;
}

internal static IOperationBinding LoadOperationBinding(ParseNode node)
{
var property = node as PropertyNode;
try
{
if (node.Context.OperationBindingParsers.TryGetValue(property.Name, out var parser))
{
return parser.LoadBinding(property);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = node.Context.GetLocation();
node.Context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}

return null;
return BindingDeserializer.LoadBindings(mapNode, node.Context.OperationBindingParsers, operationBindingPatternFields);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;

internal static partial class AsyncApiV2Deserializer
{
private static readonly PatternFieldMap<AsyncApiBindings<IServerBinding>> serverBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

internal static AsyncApiBindings<IServerBinding> LoadServerBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("serverBindings");
Expand All @@ -16,42 +22,7 @@ internal static AsyncApiBindings<IServerBinding> LoadServerBindings(ParseNode no
return new AsyncApiBindingsReference<IServerBinding>(pointer);
}

var serverBindings = new AsyncApiBindings<IServerBinding>();
foreach (var property in mapNode)
{
var serverBinding = LoadServerBinding(property);

if (serverBinding != null)
{
serverBindings.Add(serverBinding);
}
else
{
mapNode.Context.Diagnostic.Errors.Add(
new AsyncApiError(node.Context.GetLocation(), $"ServerBinding {property.Name} is not found"));
}
}

return serverBindings;
}

internal static IServerBinding LoadServerBinding(ParseNode node)
{
var property = node as PropertyNode;
try
{
if (node.Context.ServerBindingParsers.TryGetValue(property.Name, out var parser))
{
return parser.LoadBinding(property);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = node.Context.GetLocation();
node.Context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}

return null;
return BindingDeserializer.LoadBindings(mapNode, node.Context.ServerBindingParsers, serverBindingPatternFields);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
namespace ByteBard.AsyncAPI.Readers
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Exceptions;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.ParseNodes;

internal static partial class AsyncApiV3Deserializer
{
private static readonly PatternFieldMap<AsyncApiBindings<IChannelBinding>> channelBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("channelBindings");
Expand All @@ -17,50 +22,7 @@ internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode
return new AsyncApiBindingsReference<IChannelBinding>(pointer);
}

var channelBindings = new AsyncApiBindings<IChannelBinding>();
foreach (var property in mapNode)
{
var channelBinding = LoadChannelBinding(property);

if (channelBinding != null)
{
channelBindings.Add(channelBinding);
}
else
{
mapNode.Context.Diagnostic.Errors.Add(
new AsyncApiError(node.Context.GetLocation(), $"ChannelBinding {property.Name} is not found"));
}
}

mapNode.ParseFields(channelBindings, channelBindingPatternFields);

return channelBindings;
}

private static readonly PatternFieldMap<AsyncApiBindings<IChannelBinding>> channelBindingPatternFields =
new()
{
{ s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n)) },
};

private static IChannelBinding LoadChannelBinding(ParseNode node)
{
var property = node as PropertyNode;
try
{
if (node.Context.ChannelBindingParsers.TryGetValue(property.Name, out var parser))
{
return parser.LoadBinding(property);
}
}
catch (AsyncApiException ex)
{
ex.Pointer = node.Context.GetLocation();
node.Context.Diagnostic.Errors.Add(new AsyncApiError(ex));
}

return null;
return BindingDeserializer.LoadBindings(mapNode, node.Context.ChannelBindingParsers, channelBindingPatternFields);
}
}
}
Loading
Loading