Skip to content

Commit

Permalink
refactor: improve bindings TryGetValue (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean authored Jul 29, 2024
1 parent fb50d00 commit a554e41
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@ namespace LEGO.AsyncAPI.Models
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

public static class BindingExtensions
{
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IServerBinding> bindings, out IServerBinding binding)
where TBinding : IServerBinding
{
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IChannelBinding> bindings, out IChannelBinding binding)
where TBinding : IChannelBinding
{
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IOperationBinding> bindings, out IOperationBinding binding)
where TBinding : IOperationBinding
{
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IMessageBinding> bindings, out IMessageBinding binding)
where TBinding : IMessageBinding
{
return bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out binding);
}
}

public class AsyncApiBindings<TBinding> : Dictionary<string, TBinding>, IAsyncApiReferenceable
where TBinding : IBinding
{
Expand Down
62 changes: 62 additions & 0 deletions src/LEGO.AsyncAPI/Models/BindingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Models
{
using System;
using LEGO.AsyncAPI.Models.Interfaces;

public static class BindingExtensions
{
public static bool TryGetValue<TBinding>(this AsyncApiBindings<IServerBinding> bindings, out TBinding binding)
where TBinding : class, IServerBinding
{
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var serverBinding))
{
binding = serverBinding as TBinding;
return true;
}

binding = default;
return false;
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IChannelBinding> bindings, out TBinding binding)
where TBinding : class, IChannelBinding
{
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var channelBinding))
{
binding = channelBinding as TBinding;
return true;
}

binding = default;
return false;
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IOperationBinding> bindings, out TBinding binding)
where TBinding : class, IOperationBinding
{
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var operationBinding))
{
binding = operationBinding as TBinding;
return true;
}

binding = default;
return false;
}

public static bool TryGetValue<TBinding>(this AsyncApiBindings<IMessageBinding> bindings, out TBinding binding)
where TBinding : class, IMessageBinding
{
if (bindings.TryGetValue(Activator.CreateInstance<TBinding>().BindingKey, out var messageBinding))
{
binding = messageBinding as TBinding;
return true;
}

binding = default;
return false;
}
}
}
89 changes: 89 additions & 0 deletions test/LEGO.AsyncAPI.Tests/Bindings/BindingExtensions_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Tests.Bindings.WebSockets
{
using System.Linq;
using FluentAssertions;
using LEGO.AsyncAPI.Bindings.MQTT;
using LEGO.AsyncAPI.Bindings.Pulsar;
using LEGO.AsyncAPI.Bindings.WebSockets;
using LEGO.AsyncAPI.Models;
using NUnit.Framework;

public class BindingExtensions_Should
{
[Test]
public void TryGetValue_WithChannelBinding_ReturnsBinding()
{
var channel = new AsyncApiChannel();
channel.Bindings.Add(new WebSocketsChannelBinding
{
Method = "POST",
Query = new AsyncApiSchema
{
Description = "this mah query",
},
Headers = new AsyncApiSchema
{
Description = "this mah binding",
},
});

var result = channel.Bindings.TryGetValue<WebSocketsChannelBinding>(out var channelBinding);
result.Should().BeTrue();
channelBinding.Should().NotBeNull();
channelBinding.Should().BeEquivalentTo(channel.Bindings.First().Value);
}

[Test]
public void TryGetValue_WithServerBinding_ReturnsBinding()
{
var server = new AsyncApiServer();
server.Bindings.Add(new PulsarServerBinding
{
Tenant = "test tenant",
});

var result = server.Bindings.TryGetValue<PulsarServerBinding>(out var serverBinding);
result.Should().BeTrue();
serverBinding.Should().NotBeNull();
serverBinding.Should().BeEquivalentTo(server.Bindings.First().Value);
}

[Test]
public void TryGetValue_WithOperationBinding_ReturnsBinding()
{
var operation = new AsyncApiOperation();
operation.Bindings.Add(new MQTTOperationBinding
{
QoS = 23,
MessageExpiryInterval = 1,
Retain = true,
});

var result = operation.Bindings.TryGetValue<MQTTOperationBinding>(out var operationBinding);
result.Should().BeTrue();
operationBinding.Should().NotBeNull();
operationBinding.Should().BeEquivalentTo(operation.Bindings.First().Value);
}

[Test]
public void TryGetValue_WithMessageBinding_ReturnsBinding()
{
var message = new AsyncApiMessage();
message.Bindings.Add(new MQTTMessageBinding
{
PayloadFormatIndicator = 2,
CorrelationData = new AsyncApiSchema
{
Description = "Test",
},
});

var result = message.Bindings.TryGetValue<MQTTMessageBinding>(out var messageBinding);
result.Should().BeTrue();
messageBinding.Should().NotBeNull();
messageBinding.Should().BeEquivalentTo(message.Bindings.First().Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace LEGO.AsyncAPI.Tests.Bindings.WebSockets
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Readers;
using NUnit.Framework;

internal class WebSocketBindings_Should : TestBase
public class WebSocketBindings_Should : TestBase
{
[Test]
public void WebSocketChannelBinding_WithFilledObject_SerializesAndDeserializes()
Expand Down

0 comments on commit a554e41

Please sign in to comment.