Skip to content

feat(bindings): update FilterPolicy to match AWS API #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,8 +21,14 @@ public class Consumer : IAsyncApiExtensible

/// <summary>
/// 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.
/// </summary>
public FilterPolicy FilterPolicy { get; set; }
public IAsyncApiAny FilterPolicy { get; set; }

/// <summary>
/// Determines whether the FilterPolicy applies to MessageAttributes or MessageBody.
/// </summary>
public FilterPolicyScope FilterPolicyScope { get; set; }

/// <summary>
/// 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.
Expand Down Expand Up @@ -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));
Expand All @@ -77,4 +85,10 @@ public enum Protocol
[Display("lambda")] Lambda,
[Display("firehose")] Firehose,
}

public enum FilterPolicyScope
{
[Display("MessageAttributes")] MessageAttributes,
[Display("MessageBody")] MessageBody,
}
}
30 changes: 0 additions & 30 deletions src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs

This file was deleted.

8 changes: 2 additions & 6 deletions src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ public class SnsOperationBinding : OperationBinding<SnsOperationBinding>
{
{ "protocol", (a, n) => { a.Protocol = n.GetScalarValue().GetEnumFromDisplayName<Protocol>(); } },
{ "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<FilterPolicyScope>(); } },
{ "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<FilterPolicy> filterPolicyFixedFields => new()
{
{ "attributes", (a, n) => { a.Attributes = n.CreateAny(); } },
};

private FixedFieldMap<RedrivePolicy> redrivePolicyFixedFields => new()
{
{ "deadLetterQueue", (a, n) => { a.DeadLetterQueue = n.ParseMapWithExtensions(identifierFixFields); } },
Expand Down
78 changes: 32 additions & 46 deletions test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<string, IAsyncApiExtension>()
{
"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()
{
Expand Down