From 5b6465474ae09d42a27377bf04d58fdbd1dd8a59 Mon Sep 17 00:00:00 2001 From: Gadam8 <44494964+Gadam8@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:38:57 +0100 Subject: [PATCH] feat(bindings): update FilterPolicy to match AWS API (#128) Co-authored-by: adam.gloyne --- src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs | 18 ++++- .../Sns/FilterPolicy.cs | 30 ------- .../Sns/SnsOperationBinding.cs | 8 +- .../Bindings/Sns/SnsBindings_Should.cs | 78 ++++++++----------- 4 files changed, 50 insertions(+), 84 deletions(-) delete mode 100644 src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs diff --git a/src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs b/src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs index 46548977..a38174bc 100644 --- a/src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs +++ b/src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs @@ -3,6 +3,7 @@ namespace LEGO.AsyncAPI.Bindings.Sns using System; using System.Collections.Generic; using LEGO.AsyncAPI.Attributes; + using LEGO.AsyncAPI.Models.Any; using LEGO.AsyncAPI.Models.Interfaces; using LEGO.AsyncAPI.Writers; @@ -20,8 +21,14 @@ public class Consumer : IAsyncApiExtensible /// /// Only receive a subset of messages from the channel, determined by this policy. + /// Depending on the FilterPolicyScope, a map of either a message attribute or message body to an array of possible matches. The match may be a simple string for an exact match, but it may also be an object that represents a constraint and values for that constraint. /// - public FilterPolicy FilterPolicy { get; set; } + public IAsyncApiAny FilterPolicy { get; set; } + + /// + /// Determines whether the FilterPolicy applies to MessageAttributes or MessageBody. + /// + public FilterPolicyScope FilterPolicyScope { get; set; } /// /// If true AWS SNS attributes are removed from the body, and for SQS, SNS message attributes are copied to SQS message attributes. If false the SNS attributes are included in the body. @@ -55,7 +62,8 @@ public void Serialize(IAsyncApiWriter writer) writer.WriteStartObject(); writer.WriteRequiredProperty("protocol", this.Protocol.GetDisplayName()); writer.WriteRequiredObject("endpoint", this.Endpoint, (w, e) => e.Serialize(w)); - writer.WriteOptionalObject("filterPolicy", this.FilterPolicy, (w, f) => f.Serialize(w)); + writer.WriteOptionalObject("filterPolicy", this.FilterPolicy, (w, f) => f.Write(w)); + writer.WriteOptionalProperty("filterPolicyScope", this.FilterPolicyScope.GetDisplayName()); writer.WriteRequiredProperty("rawMessageDelivery", this.RawMessageDelivery); writer.WriteOptionalObject("redrivePolicy", this.RedrivePolicy, (w, p) => p.Serialize(w)); writer.WriteOptionalObject("deliveryPolicy", this.DeliveryPolicy, (w, p) => p.Serialize(w)); @@ -77,4 +85,10 @@ public enum Protocol [Display("lambda")] Lambda, [Display("firehose")] Firehose, } + + public enum FilterPolicyScope + { + [Display("MessageAttributes")] MessageAttributes, + [Display("MessageBody")] MessageBody, + } } \ No newline at end of file diff --git a/src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs b/src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs deleted file mode 100644 index 47530cc0..00000000 --- a/src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace LEGO.AsyncAPI.Bindings.Sns -{ - using System; - using System.Collections.Generic; - using LEGO.AsyncAPI.Models.Interfaces; - using LEGO.AsyncAPI.Writers; - - public class FilterPolicy : IAsyncApiExtensible - { - /// - /// A map of a message attribute to an array of possible matches. The match may be a simple string for an exact match, but it may also be an object that represents a constraint and values for that constraint. - /// - public IAsyncApiAny Attributes { get; set; } - - public IDictionary Extensions { get; set; } = new Dictionary(); - - public void Serialize(IAsyncApiWriter writer) - { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - writer.WriteStartObject(); - writer.WriteRequiredObject("attributes", this.Attributes, (w, a) => w.WriteAny(a)); - writer.WriteExtensions(this.Extensions); - writer.WriteEndObject(); - } - } -} \ No newline at end of file diff --git a/src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs b/src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs index d35a46f5..da077f2d 100644 --- a/src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs +++ b/src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs @@ -48,18 +48,14 @@ public class SnsOperationBinding : OperationBinding { { "protocol", (a, n) => { a.Protocol = n.GetScalarValue().GetEnumFromDisplayName(); } }, { "endpoint", (a, n) => { a.Endpoint = n.ParseMapWithExtensions(this.identifierFixFields); } }, - { "filterPolicy", (a, n) => { a.FilterPolicy = n.ParseMapWithExtensions(this.filterPolicyFixedFields); } }, + { "filterPolicy", (a, n) => { a.FilterPolicy = n.CreateAny(); } }, + { "filterPolicyScope", (a, n) => { a.FilterPolicyScope = n.GetScalarValue().GetEnumFromDisplayName(); } }, { "rawMessageDelivery", (a, n) => { a.RawMessageDelivery = n.GetBooleanValue(); } }, { "redrivePolicy", (a, n) => { a.RedrivePolicy = n.ParseMapWithExtensions(this.redrivePolicyFixedFields); } }, { "deliveryPolicy", (a, n) => { a.DeliveryPolicy = n.ParseMapWithExtensions(this.deliveryPolicyFixedFields); } }, { "displayName", (a, n) => { a.DisplayName = n.GetScalarValue(); } }, }; - private FixedFieldMap filterPolicyFixedFields => new() - { - { "attributes", (a, n) => { a.Attributes = n.CreateAny(); } }, - }; - private FixedFieldMap redrivePolicyFixedFields => new() { { "deadLetterQueue", (a, n) => { a.DeadLetterQueue = n.ParseMapWithExtensions(identifierFixFields); } }, diff --git a/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs b/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs index 7a2269bb..948dbb7e 100644 --- a/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs +++ b/test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs @@ -167,20 +167,18 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes() x-identifierExtension: identifierXPropertyName: identifierXPropertyValue filterPolicy: - attributes: - store: - - asyncapi_corp - contact: dec.kolakowski - event: - - anything-but: order_cancelled - order_key: - transient: by_area - customer_interests: - - rugby - - football - - baseball - x-filterPolicyExtension: - filterPolicyXPropertyName: filterPolicyXPropertyValue + store: + - asyncapi_corp + contact: dec.kolakowski + event: + - anything-but: order_cancelled + order_key: + transient: by_area + customer_interests: + - rugby + - football + - baseball + filterPolicyScope: MessageAttributes rawMessageDelivery: false redrivePolicy: deadLetterQueue: @@ -253,47 +251,35 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes() }, }, }, - FilterPolicy = new FilterPolicy() - { - Attributes = new AsyncApiObject() + FilterPolicy = new AsyncApiObject() + { + { "store", new AsyncApiArray() { new AsyncApiString("asyncapi_corp") } }, + { "contact", new AsyncApiString("dec.kolakowski") }, { - { "store", new AsyncApiArray() { new AsyncApiString("asyncapi_corp") } }, - { "contact", new AsyncApiString("dec.kolakowski") }, - { - "event", new AsyncApiArray() - { - new AsyncApiObject() - { - { "anything-but", new AsyncApiString("order_cancelled") }, - }, - } - }, + "event", new AsyncApiArray() { - "order_key", new AsyncApiObject() + new AsyncApiObject() { - { "transient", new AsyncApiString("by_area") }, - } - }, + { "anything-but", new AsyncApiString("order_cancelled") }, + }, + } + }, + { + "order_key", new AsyncApiObject() { - "customer_interests", new AsyncApiArray() - { - new AsyncApiString("rugby"), - new AsyncApiString("football"), - new AsyncApiString("baseball"), - } - }, + { "transient", new AsyncApiString("by_area") }, + } }, - Extensions = new Dictionary() { + "customer_interests", new AsyncApiArray() { - "x-filterPolicyExtension", - new AsyncApiObject() - { - { "filterPolicyXPropertyName", new AsyncApiString("filterPolicyXPropertyValue") }, - } - }, + new AsyncApiString("rugby"), + new AsyncApiString("football"), + new AsyncApiString("baseball"), + } }, }, + FilterPolicyScope = FilterPolicyScope.MessageAttributes, RawMessageDelivery = false, RedrivePolicy = new RedrivePolicy() {