Skip to content

Commit 6816cd1

Browse files
committed
binding references done
1 parent fedf798 commit 6816cd1

13 files changed

+312
-82
lines changed

src/LEGO.AsyncAPI.Readers/V2/AsyncApiChannelBindingDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static AsyncApiBindings<IChannelBinding> LoadChannelBindings(ParseNode
1515
var pointer = mapNode.GetReferencePointer();
1616
if (pointer != null)
1717
{
18-
return mapNode.GetReferencedObject<AsyncApiBindings<IChannelBinding>>(ReferenceType.ChannelBindings, pointer);
18+
return new AsyncApiBindingsReference<IChannelBinding>(pointer);
1919
}
2020

2121
var channelBindings = new AsyncApiBindings<IChannelBinding>();

src/LEGO.AsyncAPI.Readers/V2/AsyncApiComponentsDeserializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ internal static partial class AsyncApiV2Deserializer
1919
{ "correlationIds", (a, n) => a.CorrelationIds = n.CreateMap(LoadCorrelationId) },
2020
{ "operationTraits", (a, n) => a.OperationTraits = n.CreateMap(LoadOperationTrait) },
2121
{ "messageTraits", (a, n) => a.MessageTraits = n.CreateMap(LoadMessageTrait) },
22-
{ "serverBindings", (a, n) => a.ServerBindings = n.CreateMapWithReference(ReferenceType.ServerBindings, LoadServerBindings) },
23-
{ "channelBindings", (a, n) => a.ChannelBindings = n.CreateMapWithReference(ReferenceType.ChannelBindings, LoadChannelBindings) },
24-
{ "operationBindings", (a, n) => a.OperationBindings = n.CreateMapWithReference(ReferenceType.OperationBindings, LoadOperationBindings) },
25-
{ "messageBindings", (a, n) => a.MessageBindings = n.CreateMapWithReference(ReferenceType.MessageBindings, LoadMessageBindings) },
22+
{ "serverBindings", (a, n) => a.ServerBindings = n.CreateMap(LoadServerBindings) },
23+
{ "channelBindings", (a, n) => a.ChannelBindings = n.CreateMap(LoadChannelBindings) },
24+
{ "operationBindings", (a, n) => a.OperationBindings = n.CreateMap(LoadOperationBindings) },
25+
{ "messageBindings", (a, n) => a.MessageBindings = n.CreateMap(LoadMessageBindings) },
2626
};
2727

2828
private static PatternFieldMap<AsyncApiComponents> componentsPatternFields =

src/LEGO.AsyncAPI.Readers/V2/AsyncApiMessageBindingDeserializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ internal static partial class AsyncApiV2Deserializer
1212
internal static AsyncApiBindings<IMessageBinding> LoadMessageBindings(ParseNode node)
1313
{
1414
var mapNode = node.CheckMapNode("messageBindings");
15+
var pointer = mapNode.GetReferencePointer();
16+
if (pointer != null)
17+
{
18+
return new AsyncApiBindingsReference<IMessageBinding>(pointer);
19+
}
1520

1621
var messageBindings = new AsyncApiBindings<IMessageBinding>();
17-
1822
foreach (var property in mapNode)
1923
{
2024
var messageBinding = LoadMessageBinding(property);

src/LEGO.AsyncAPI.Readers/V2/AsyncApiOperationBindingDeserializer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ internal static partial class AsyncApiV2Deserializer
1212
internal static AsyncApiBindings<IOperationBinding> LoadOperationBindings(ParseNode node)
1313
{
1414
var mapNode = node.CheckMapNode("operationBindings");
15+
var pointer = mapNode.GetReferencePointer();
16+
if (pointer != null)
17+
{
18+
return new AsyncApiBindingsReference<IOperationBinding>(pointer);
19+
}
1520

1621
var operationBindings = new AsyncApiBindings<IOperationBinding>();
17-
1822
foreach (var property in mapNode)
1923
{
2024
var operationBinding = LoadOperationBinding(property);

src/LEGO.AsyncAPI.Readers/V2/AsyncApiServerBindingDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static AsyncApiBindings<IServerBinding> LoadServerBindings(ParseNode no
1515
var pointer = mapNode.GetReferencePointer();
1616
if (pointer != null)
1717
{
18-
return mapNode.GetReferencedObject<AsyncApiBindings<IServerBinding>>(ReferenceType.ServerBindings, pointer);
18+
return new AsyncApiBindingsReference<IServerBinding>(pointer);
1919
}
2020

2121
var serverBindings = new AsyncApiBindings<IServerBinding>();

src/LEGO.AsyncAPI/Models/AsyncApiBinding.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ namespace LEGO.AsyncAPI.Bindings
44
{
55
using System;
66
using System.Collections.Generic;
7-
using LEGO.AsyncAPI.Models;
87
using LEGO.AsyncAPI.Models.Interfaces;
98
using LEGO.AsyncAPI.Writers;
109

1110
public abstract class AsyncApiBinding : IBinding
1211
{
1312
public abstract string BindingKey { get; }
1413

15-
public bool UnresolvedReference { get; set; }
16-
17-
public AsyncApiReference Reference { get; set; }
18-
1914
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
2015

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

30-
if (this.Reference != null && !writer.GetSettings().ShouldInlineReference(this.Reference))
31-
{
32-
this.Reference.SerializeV2(writer);
33-
return;
34-
}
35-
3625
this.SerializeProperties(writer);
3726
}
3827

src/LEGO.AsyncAPI/Models/AsyncApiBindings{TBinding}.cs

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,17 @@
33
namespace LEGO.AsyncAPI.Models
44
{
55
using System;
6+
using System.Collections;
67
using System.Collections.Generic;
78
using LEGO.AsyncAPI.Models.Interfaces;
89
using LEGO.AsyncAPI.Writers;
910

10-
public class AsyncApiBindings<TBinding> : Dictionary<string, TBinding>, IAsyncApiReferenceable
11+
public class AsyncApiBindings<TBinding> : IDictionary<string, TBinding>, IAsyncApiSerializable
1112
where TBinding : IBinding
1213
{
13-
public bool UnresolvedReference { get; set; }
14+
private Dictionary<string, TBinding> inner = new Dictionary<string, TBinding>();
1415

15-
public AsyncApiReference Reference { get; set; }
16-
17-
public void Add(TBinding binding)
18-
{
19-
this[binding.BindingKey] = binding;
20-
}
21-
22-
public void SerializeV2(IAsyncApiWriter writer)
23-
{
24-
if (writer is null)
25-
{
26-
throw new ArgumentNullException(nameof(writer));
27-
}
28-
29-
if (this.Reference != null && !writer.GetSettings().ShouldInlineReference(this.Reference))
30-
{
31-
this.Reference.SerializeV2(writer);
32-
return;
33-
}
34-
35-
this.SerializeV2WithoutReference(writer);
36-
}
37-
38-
public void SerializeV2WithoutReference(IAsyncApiWriter writer)
16+
public virtual void SerializeV2(IAsyncApiWriter writer)
3917
{
4018
if (writer is null)
4119
{
@@ -56,5 +34,79 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)
5634

5735
writer.WriteEndObject();
5836
}
37+
38+
public virtual void Add(TBinding binding)
39+
{
40+
this[binding.BindingKey] = binding;
41+
}
42+
43+
public virtual TBinding this[string key]
44+
{
45+
get => inner[key];
46+
set => inner[key] = value;
47+
}
48+
49+
public virtual ICollection<string> Keys => inner.Keys;
50+
51+
public virtual ICollection<TBinding> Values => inner.Values;
52+
53+
public virtual int Count => inner.Count;
54+
55+
public virtual bool IsReadOnly => ((IDictionary<string, TBinding>)inner).IsReadOnly;
56+
57+
public virtual void Add(string key, TBinding value)
58+
{
59+
inner.Add(key, value);
60+
}
61+
62+
public virtual bool ContainsKey(string key)
63+
{
64+
return inner.ContainsKey(key);
65+
}
66+
67+
public virtual bool Remove(string key)
68+
{
69+
return inner.Remove(key);
70+
}
71+
72+
public virtual bool TryGetValue(string key, out TBinding value)
73+
{
74+
return inner.TryGetValue(key, out value);
75+
}
76+
77+
public virtual void Add(KeyValuePair<string, TBinding> item)
78+
{
79+
((IDictionary<string, TBinding>)inner).Add(item);
80+
}
81+
82+
public virtual void Clear()
83+
{
84+
inner.Clear();
85+
}
86+
87+
public virtual bool Contains(KeyValuePair<string, TBinding> item)
88+
{
89+
return ((IDictionary<string, TBinding>)inner).Contains(item);
90+
}
91+
92+
public virtual void CopyTo(KeyValuePair<string, TBinding>[] array, int arrayIndex)
93+
{
94+
((IDictionary<string, TBinding>)inner).CopyTo(array, arrayIndex);
95+
}
96+
97+
public virtual bool Remove(KeyValuePair<string, TBinding> item)
98+
{
99+
return ((IDictionary<string, TBinding>)inner).Remove(item);
100+
}
101+
102+
public virtual IEnumerator<KeyValuePair<string, TBinding>> GetEnumerator()
103+
{
104+
return inner.GetEnumerator();
105+
}
106+
107+
IEnumerator IEnumerable.GetEnumerator()
108+
{
109+
return inner.GetEnumerator();
110+
}
59111
}
60112
}

src/LEGO.AsyncAPI/Models/AsyncApiComponents.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,9 @@ public void SerializeV2(IAsyncApiWriter writer)
290290
this.ServerBindings,
291291
(w, key, component) =>
292292
{
293-
if (component.Reference != null &&
294-
component.Reference.Type == ReferenceType.ServerBindings &&
295-
component.Reference.FragmentId == key)
293+
if (component is AsyncApiBindingsReference<IServerBinding> reference)
296294
{
297-
component.SerializeV2WithoutReference(w);
295+
reference.SerializeV2(w);
298296
}
299297
else
300298
{
@@ -308,11 +306,9 @@ public void SerializeV2(IAsyncApiWriter writer)
308306
this.ChannelBindings,
309307
(w, key, component) =>
310308
{
311-
if (component.Reference != null &&
312-
component.Reference.Type == ReferenceType.ChannelBindings &&
313-
component.Reference.FragmentId == key)
309+
if (component is AsyncApiBindingsReference<IChannelBinding> reference)
314310
{
315-
component.SerializeV2WithoutReference(w);
311+
reference.SerializeV2(w);
316312
}
317313
else
318314
{
@@ -326,11 +322,9 @@ public void SerializeV2(IAsyncApiWriter writer)
326322
this.OperationBindings,
327323
(w, key, component) =>
328324
{
329-
if (component.Reference != null &&
330-
component.Reference.Type == ReferenceType.OperationBindings &&
331-
component.Reference.FragmentId == key)
325+
if (component is AsyncApiBindingsReference<IOperationBinding> reference)
332326
{
333-
component.SerializeV2WithoutReference(w);
327+
reference.SerializeV2(w);
334328
}
335329
else
336330
{
@@ -344,11 +338,9 @@ public void SerializeV2(IAsyncApiWriter writer)
344338
this.MessageBindings,
345339
(w, key, component) =>
346340
{
347-
if (component.Reference != null &&
348-
component.Reference.Type == ReferenceType.MessageBindings &&
349-
component.Reference.FragmentId == key)
341+
if (component is AsyncApiBindingsReference<IMessageBinding> reference)
350342
{
351-
component.SerializeV2WithoutReference(w);
343+
reference.SerializeV2(w);
352344
}
353345
else
354346
{

0 commit comments

Comments
 (0)