Skip to content

Commit 5b64654

Browse files
Gadam8adam.gloyne
andauthored
feat(bindings): update FilterPolicy to match AWS API (#128)
Co-authored-by: adam.gloyne <[email protected]>
1 parent 32ff8df commit 5b64654

File tree

4 files changed

+50
-84
lines changed

4 files changed

+50
-84
lines changed

src/LEGO.AsyncAPI.Bindings/Sns/Consumer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace LEGO.AsyncAPI.Bindings.Sns
33
using System;
44
using System.Collections.Generic;
55
using LEGO.AsyncAPI.Attributes;
6+
using LEGO.AsyncAPI.Models.Any;
67
using LEGO.AsyncAPI.Models.Interfaces;
78
using LEGO.AsyncAPI.Writers;
89

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

2122
/// <summary>
2223
/// Only receive a subset of messages from the channel, determined by this policy.
24+
/// 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.
2325
/// </summary>
24-
public FilterPolicy FilterPolicy { get; set; }
26+
public IAsyncApiAny FilterPolicy { get; set; }
27+
28+
/// <summary>
29+
/// Determines whether the FilterPolicy applies to MessageAttributes or MessageBody.
30+
/// </summary>
31+
public FilterPolicyScope FilterPolicyScope { get; set; }
2532

2633
/// <summary>
2734
/// 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)
5562
writer.WriteStartObject();
5663
writer.WriteRequiredProperty("protocol", this.Protocol.GetDisplayName());
5764
writer.WriteRequiredObject("endpoint", this.Endpoint, (w, e) => e.Serialize(w));
58-
writer.WriteOptionalObject("filterPolicy", this.FilterPolicy, (w, f) => f.Serialize(w));
65+
writer.WriteOptionalObject("filterPolicy", this.FilterPolicy, (w, f) => f.Write(w));
66+
writer.WriteOptionalProperty("filterPolicyScope", this.FilterPolicyScope.GetDisplayName());
5967
writer.WriteRequiredProperty("rawMessageDelivery", this.RawMessageDelivery);
6068
writer.WriteOptionalObject("redrivePolicy", this.RedrivePolicy, (w, p) => p.Serialize(w));
6169
writer.WriteOptionalObject("deliveryPolicy", this.DeliveryPolicy, (w, p) => p.Serialize(w));
@@ -77,4 +85,10 @@ public enum Protocol
7785
[Display("lambda")] Lambda,
7886
[Display("firehose")] Firehose,
7987
}
88+
89+
public enum FilterPolicyScope
90+
{
91+
[Display("MessageAttributes")] MessageAttributes,
92+
[Display("MessageBody")] MessageBody,
93+
}
8094
}

src/LEGO.AsyncAPI.Bindings/Sns/FilterPolicy.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/LEGO.AsyncAPI.Bindings/Sns/SnsOperationBinding.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,14 @@ public class SnsOperationBinding : OperationBinding<SnsOperationBinding>
4848
{
4949
{ "protocol", (a, n) => { a.Protocol = n.GetScalarValue().GetEnumFromDisplayName<Protocol>(); } },
5050
{ "endpoint", (a, n) => { a.Endpoint = n.ParseMapWithExtensions(this.identifierFixFields); } },
51-
{ "filterPolicy", (a, n) => { a.FilterPolicy = n.ParseMapWithExtensions(this.filterPolicyFixedFields); } },
51+
{ "filterPolicy", (a, n) => { a.FilterPolicy = n.CreateAny(); } },
52+
{ "filterPolicyScope", (a, n) => { a.FilterPolicyScope = n.GetScalarValue().GetEnumFromDisplayName<FilterPolicyScope>(); } },
5253
{ "rawMessageDelivery", (a, n) => { a.RawMessageDelivery = n.GetBooleanValue(); } },
5354
{ "redrivePolicy", (a, n) => { a.RedrivePolicy = n.ParseMapWithExtensions(this.redrivePolicyFixedFields); } },
5455
{ "deliveryPolicy", (a, n) => { a.DeliveryPolicy = n.ParseMapWithExtensions(this.deliveryPolicyFixedFields); } },
5556
{ "displayName", (a, n) => { a.DisplayName = n.GetScalarValue(); } },
5657
};
5758

58-
private FixedFieldMap<FilterPolicy> filterPolicyFixedFields => new()
59-
{
60-
{ "attributes", (a, n) => { a.Attributes = n.CreateAny(); } },
61-
};
62-
6359
private FixedFieldMap<RedrivePolicy> redrivePolicyFixedFields => new()
6460
{
6561
{ "deadLetterQueue", (a, n) => { a.DeadLetterQueue = n.ParseMapWithExtensions(identifierFixFields); } },

test/LEGO.AsyncAPI.Tests/Bindings/Sns/SnsBindings_Should.cs

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,18 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes()
167167
x-identifierExtension:
168168
identifierXPropertyName: identifierXPropertyValue
169169
filterPolicy:
170-
attributes:
171-
store:
172-
- asyncapi_corp
173-
contact: dec.kolakowski
174-
event:
175-
- anything-but: order_cancelled
176-
order_key:
177-
transient: by_area
178-
customer_interests:
179-
- rugby
180-
- football
181-
- baseball
182-
x-filterPolicyExtension:
183-
filterPolicyXPropertyName: filterPolicyXPropertyValue
170+
store:
171+
- asyncapi_corp
172+
contact: dec.kolakowski
173+
event:
174+
- anything-but: order_cancelled
175+
order_key:
176+
transient: by_area
177+
customer_interests:
178+
- rugby
179+
- football
180+
- baseball
181+
filterPolicyScope: MessageAttributes
184182
rawMessageDelivery: false
185183
redrivePolicy:
186184
deadLetterQueue:
@@ -253,47 +251,35 @@ public void SnsOperationBinding_WithFilledObject_SerializesAndDeserializes()
253251
},
254252
},
255253
},
256-
FilterPolicy = new FilterPolicy()
257-
{
258-
Attributes = new AsyncApiObject()
254+
FilterPolicy = new AsyncApiObject()
255+
{
256+
{ "store", new AsyncApiArray() { new AsyncApiString("asyncapi_corp") } },
257+
{ "contact", new AsyncApiString("dec.kolakowski") },
259258
{
260-
{ "store", new AsyncApiArray() { new AsyncApiString("asyncapi_corp") } },
261-
{ "contact", new AsyncApiString("dec.kolakowski") },
262-
{
263-
"event", new AsyncApiArray()
264-
{
265-
new AsyncApiObject()
266-
{
267-
{ "anything-but", new AsyncApiString("order_cancelled") },
268-
},
269-
}
270-
},
259+
"event", new AsyncApiArray()
271260
{
272-
"order_key", new AsyncApiObject()
261+
new AsyncApiObject()
273262
{
274-
{ "transient", new AsyncApiString("by_area") },
275-
}
276-
},
263+
{ "anything-but", new AsyncApiString("order_cancelled") },
264+
},
265+
}
266+
},
267+
{
268+
"order_key", new AsyncApiObject()
277269
{
278-
"customer_interests", new AsyncApiArray()
279-
{
280-
new AsyncApiString("rugby"),
281-
new AsyncApiString("football"),
282-
new AsyncApiString("baseball"),
283-
}
284-
},
270+
{ "transient", new AsyncApiString("by_area") },
271+
}
285272
},
286-
Extensions = new Dictionary<string, IAsyncApiExtension>()
287273
{
274+
"customer_interests", new AsyncApiArray()
288275
{
289-
"x-filterPolicyExtension",
290-
new AsyncApiObject()
291-
{
292-
{ "filterPolicyXPropertyName", new AsyncApiString("filterPolicyXPropertyValue") },
293-
}
294-
},
276+
new AsyncApiString("rugby"),
277+
new AsyncApiString("football"),
278+
new AsyncApiString("baseball"),
279+
}
295280
},
296281
},
282+
FilterPolicyScope = FilterPolicyScope.MessageAttributes,
297283
RawMessageDelivery = false,
298284
RedrivePolicy = new RedrivePolicy()
299285
{

0 commit comments

Comments
 (0)