Skip to content

Commit a554e41

Browse files
authored
refactor: improve bindings TryGetValue (#190)
1 parent fb50d00 commit a554e41

File tree

4 files changed

+153
-29
lines changed

4 files changed

+153
-29
lines changed

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,6 @@ namespace LEGO.AsyncAPI.Models
77
using LEGO.AsyncAPI.Models.Interfaces;
88
using LEGO.AsyncAPI.Writers;
99

10-
public static class BindingExtensions
11-
{
12-
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IServerBinding> bindings, out IServerBinding binding)
13-
where TBinding : IServerBinding
14-
{
15-
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
16-
}
17-
18-
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IChannelBinding> bindings, out IChannelBinding binding)
19-
where TBinding : IChannelBinding
20-
{
21-
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
22-
}
23-
24-
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IOperationBinding> bindings, out IOperationBinding binding)
25-
where TBinding : IOperationBinding
26-
{
27-
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
28-
}
29-
30-
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IMessageBinding> bindings, out IMessageBinding binding)
31-
where TBinding : IMessageBinding
32-
{
33-
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
34-
}
35-
}
36-
3710
public class AsyncApiBindings<TBinding> : Dictionary<string, TBinding>, IAsyncApiReferenceable
3811
where TBinding : IBinding
3912
{
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Models
4+
{
5+
using System;
6+
using LEGO.AsyncAPI.Models.Interfaces;
7+
8+
public static class BindingExtensions
9+
{
10+
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IServerBinding> bindings, out TBinding binding)
11+
where TBinding : class, IServerBinding
12+
{
13+
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var serverBinding))
14+
{
15+
binding = serverBinding as TBinding;
16+
return true;
17+
}
18+
19+
binding = default;
20+
return false;
21+
}
22+
23+
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IChannelBinding> bindings, out TBinding binding)
24+
where TBinding : class, IChannelBinding
25+
{
26+
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var channelBinding))
27+
{
28+
binding = channelBinding as TBinding;
29+
return true;
30+
}
31+
32+
binding = default;
33+
return false;
34+
}
35+
36+
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IOperationBinding> bindings, out TBinding binding)
37+
where TBinding : class, IOperationBinding
38+
{
39+
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var operationBinding))
40+
{
41+
binding = operationBinding as TBinding;
42+
return true;
43+
}
44+
45+
binding = default;
46+
return false;
47+
}
48+
49+
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IMessageBinding> bindings, out TBinding binding)
50+
where TBinding : class, IMessageBinding
51+
{
52+
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var messageBinding))
53+
{
54+
binding = messageBinding as TBinding;
55+
return true;
56+
}
57+
58+
binding = default;
59+
return false;
60+
}
61+
}
62+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) The LEGO Group. All rights reserved.
2+
3+
namespace LEGO.AsyncAPI.Tests.Bindings.WebSockets
4+
{
5+
using System.Linq;
6+
using FluentAssertions;
7+
using LEGO.AsyncAPI.Bindings.MQTT;
8+
using LEGO.AsyncAPI.Bindings.Pulsar;
9+
using LEGO.AsyncAPI.Bindings.WebSockets;
10+
using LEGO.AsyncAPI.Models;
11+
using NUnit.Framework;
12+
13+
public class BindingExtensions_Should
14+
{
15+
[Test]
16+
public void TryGetValue_WithChannelBinding_ReturnsBinding()
17+
{
18+
var channel = new AsyncApiChannel();
19+
channel.Bindings.Add(new WebSocketsChannelBinding
20+
{
21+
Method = "POST",
22+
Query = new AsyncApiSchema
23+
{
24+
Description = "this mah query",
25+
},
26+
Headers = new AsyncApiSchema
27+
{
28+
Description = "this mah binding",
29+
},
30+
});
31+
32+
var result = channel.Bindings.TryGetValue<WebSocketsChannelBinding>(out var channelBinding);
33+
result.Should().BeTrue();
34+
channelBinding.Should().NotBeNull();
35+
channelBinding.Should().BeEquivalentTo(channel.Bindings.First().Value);
36+
}
37+
38+
[Test]
39+
public void TryGetValue_WithServerBinding_ReturnsBinding()
40+
{
41+
var server = new AsyncApiServer();
42+
server.Bindings.Add(new PulsarServerBinding
43+
{
44+
Tenant = "test tenant",
45+
});
46+
47+
var result = server.Bindings.TryGetValue<PulsarServerBinding>(out var serverBinding);
48+
result.Should().BeTrue();
49+
serverBinding.Should().NotBeNull();
50+
serverBinding.Should().BeEquivalentTo(server.Bindings.First().Value);
51+
}
52+
53+
[Test]
54+
public void TryGetValue_WithOperationBinding_ReturnsBinding()
55+
{
56+
var operation = new AsyncApiOperation();
57+
operation.Bindings.Add(new MQTTOperationBinding
58+
{
59+
QoS = 23,
60+
MessageExpiryInterval = 1,
61+
Retain = true,
62+
});
63+
64+
var result = operation.Bindings.TryGetValue<MQTTOperationBinding>(out var operationBinding);
65+
result.Should().BeTrue();
66+
operationBinding.Should().NotBeNull();
67+
operationBinding.Should().BeEquivalentTo(operation.Bindings.First().Value);
68+
}
69+
70+
[Test]
71+
public void TryGetValue_WithMessageBinding_ReturnsBinding()
72+
{
73+
var message = new AsyncApiMessage();
74+
message.Bindings.Add(new MQTTMessageBinding
75+
{
76+
PayloadFormatIndicator = 2,
77+
CorrelationData = new AsyncApiSchema
78+
{
79+
Description = "Test",
80+
},
81+
});
82+
83+
var result = message.Bindings.TryGetValue<MQTTMessageBinding>(out var messageBinding);
84+
result.Should().BeTrue();
85+
messageBinding.Should().NotBeNull();
86+
messageBinding.Should().BeEquivalentTo(message.Bindings.First().Value);
87+
}
88+
}
89+
}

test/LEGO.AsyncAPI.Tests/Bindings/WebSockets/WebSocketBindings_Should.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace LEGO.AsyncAPI.Tests.Bindings.WebSockets
88
using LEGO.AsyncAPI.Models;
99
using LEGO.AsyncAPI.Readers;
1010
using NUnit.Framework;
11-
12-
internal class WebSocketBindings_Should : TestBase
11+
12+
public class WebSocketBindings_Should : TestBase
1313
{
1414
[Test]
1515
public void WebSocketChannelBinding_WithFilledObject_SerializesAndDeserializes()

0 commit comments

Comments
 (0)