Skip to content

Commit

Permalink
binding references done
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Jan 30, 2025
1 parent fedf798 commit 6816cd1
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return mapNode.GetReferencedObject<AsyncApiBindings<IChannelBinding>>(ReferenceType.ChannelBindings, pointer);
return new AsyncApiBindingsReference<IChannelBinding>(pointer);
}

var channelBindings = new AsyncApiBindings<IChannelBinding>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ internal static partial class AsyncApiV2Deserializer
{ "correlationIds", (a, n) => a.CorrelationIds = n.CreateMap(LoadCorrelationId) },
{ "operationTraits", (a, n) => a.OperationTraits = n.CreateMap(LoadOperationTrait) },
{ "messageTraits", (a, n) => a.MessageTraits = n.CreateMap(LoadMessageTrait) },
{ "serverBindings", (a, n) => a.ServerBindings = n.CreateMapWithReference(ReferenceType.ServerBindings, LoadServerBindings) },
{ "channelBindings", (a, n) => a.ChannelBindings = n.CreateMapWithReference(ReferenceType.ChannelBindings, LoadChannelBindings) },
{ "operationBindings", (a, n) => a.OperationBindings = n.CreateMapWithReference(ReferenceType.OperationBindings, LoadOperationBindings) },
{ "messageBindings", (a, n) => a.MessageBindings = n.CreateMapWithReference(ReferenceType.MessageBindings, LoadMessageBindings) },
{ "serverBindings", (a, n) => a.ServerBindings = n.CreateMap(LoadServerBindings) },
{ "channelBindings", (a, n) => a.ChannelBindings = n.CreateMap(LoadChannelBindings) },
{ "operationBindings", (a, n) => a.OperationBindings = n.CreateMap(LoadOperationBindings) },
{ "messageBindings", (a, n) => a.MessageBindings = n.CreateMap(LoadMessageBindings) },
};

private static PatternFieldMap<AsyncApiComponents> componentsPatternFields =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ internal static partial class AsyncApiV2Deserializer
internal static AsyncApiBindings<IMessageBinding> LoadMessageBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("messageBindings");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return new AsyncApiBindingsReference<IMessageBinding>(pointer);
}

var messageBindings = new AsyncApiBindings<IMessageBinding>();

foreach (var property in mapNode)
{
var messageBinding = LoadMessageBinding(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ internal static partial class AsyncApiV2Deserializer
internal static AsyncApiBindings<IOperationBinding> LoadOperationBindings(ParseNode node)
{
var mapNode = node.CheckMapNode("operationBindings");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return new AsyncApiBindingsReference<IOperationBinding>(pointer);
}

var operationBindings = new AsyncApiBindings<IOperationBinding>();

foreach (var property in mapNode)
{
var operationBinding = LoadOperationBinding(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static AsyncApiBindings<IServerBinding> LoadServerBindings(ParseNode no
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return mapNode.GetReferencedObject<AsyncApiBindings<IServerBinding>>(ReferenceType.ServerBindings, pointer);
return new AsyncApiBindingsReference<IServerBinding>(pointer);
}

var serverBindings = new AsyncApiBindings<IServerBinding>();
Expand Down
11 changes: 0 additions & 11 deletions src/LEGO.AsyncAPI/Models/AsyncApiBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ namespace LEGO.AsyncAPI.Bindings
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public abstract class AsyncApiBinding : IBinding
{
public abstract string BindingKey { get; }

public bool UnresolvedReference { get; set; }

public AsyncApiReference Reference { get; set; }

public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();

public string BindingVersion { get; set; }
Expand All @@ -27,12 +22,6 @@ public void SerializeV2(IAsyncApiWriter writer)
throw new ArgumentNullException(nameof(writer));
}

if (this.Reference != null && !writer.GetSettings().ShouldInlineReference(this.Reference))
{
this.Reference.SerializeV2(writer);
return;
}

this.SerializeProperties(writer);
}

Expand Down
104 changes: 78 additions & 26 deletions src/LEGO.AsyncAPI/Models/AsyncApiBindings{TBinding}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@
namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public class AsyncApiBindings<TBinding> : Dictionary<string, TBinding>, IAsyncApiReferenceable
public class AsyncApiBindings<TBinding> : IDictionary<string, TBinding>, IAsyncApiSerializable
where TBinding : IBinding
{
public bool UnresolvedReference { get; set; }
private Dictionary<string, TBinding> inner = new Dictionary<string, TBinding>();

public AsyncApiReference Reference { get; set; }

public void Add(TBinding binding)
{
this[binding.BindingKey] = binding;
}

public void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

if (this.Reference != null && !writer.GetSettings().ShouldInlineReference(this.Reference))
{
this.Reference.SerializeV2(writer);
return;
}

this.SerializeV2WithoutReference(writer);
}

public void SerializeV2WithoutReference(IAsyncApiWriter writer)
public virtual void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
Expand All @@ -56,5 +34,79 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)

writer.WriteEndObject();
}

public virtual void Add(TBinding binding)
{
this[binding.BindingKey] = binding;
}

public virtual TBinding this[string key]

Check warning on line 43 in src/LEGO.AsyncAPI/Models/AsyncApiBindings{TBinding}.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

{
get => inner[key];
set => inner[key] = value;
}

public virtual ICollection<string> Keys => inner.Keys;

public virtual ICollection<TBinding> Values => inner.Values;

public virtual int Count => inner.Count;

public virtual bool IsReadOnly => ((IDictionary<string, TBinding>)inner).IsReadOnly;

public virtual void Add(string key, TBinding value)
{
inner.Add(key, value);
}

public virtual bool ContainsKey(string key)
{
return inner.ContainsKey(key);
}

public virtual bool Remove(string key)
{
return inner.Remove(key);
}

public virtual bool TryGetValue(string key, out TBinding value)
{
return inner.TryGetValue(key, out value);
}

public virtual void Add(KeyValuePair<string, TBinding> item)
{
((IDictionary<string, TBinding>)inner).Add(item);
}

public virtual void Clear()
{
inner.Clear();
}

public virtual bool Contains(KeyValuePair<string, TBinding> item)
{
return ((IDictionary<string, TBinding>)inner).Contains(item);
}

public virtual void CopyTo(KeyValuePair<string, TBinding>[] array, int arrayIndex)
{
((IDictionary<string, TBinding>)inner).CopyTo(array, arrayIndex);
}

public virtual bool Remove(KeyValuePair<string, TBinding> item)
{
return ((IDictionary<string, TBinding>)inner).Remove(item);
}

public virtual IEnumerator<KeyValuePair<string, TBinding>> GetEnumerator()
{
return inner.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return inner.GetEnumerator();
}
}
}
24 changes: 8 additions & 16 deletions src/LEGO.AsyncAPI/Models/AsyncApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,9 @@ public void SerializeV2(IAsyncApiWriter writer)
this.ServerBindings,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.ServerBindings &&
component.Reference.FragmentId == key)
if (component is AsyncApiBindingsReference<IServerBinding> reference)
{
component.SerializeV2WithoutReference(w);
reference.SerializeV2(w);
}
else
{
Expand All @@ -308,11 +306,9 @@ public void SerializeV2(IAsyncApiWriter writer)
this.ChannelBindings,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.ChannelBindings &&
component.Reference.FragmentId == key)
if (component is AsyncApiBindingsReference<IChannelBinding> reference)
{
component.SerializeV2WithoutReference(w);
reference.SerializeV2(w);
}
else
{
Expand All @@ -326,11 +322,9 @@ public void SerializeV2(IAsyncApiWriter writer)
this.OperationBindings,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.OperationBindings &&
component.Reference.FragmentId == key)
if (component is AsyncApiBindingsReference<IOperationBinding> reference)
{
component.SerializeV2WithoutReference(w);
reference.SerializeV2(w);
}
else
{
Expand All @@ -344,11 +338,9 @@ public void SerializeV2(IAsyncApiWriter writer)
this.MessageBindings,
(w, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.MessageBindings &&
component.Reference.FragmentId == key)
if (component is AsyncApiBindingsReference<IMessageBinding> reference)
{
component.SerializeV2WithoutReference(w);
reference.SerializeV2(w);
}
else
{
Expand Down
Loading

0 comments on commit 6816cd1

Please sign in to comment.