Skip to content

Commit 064ee2d

Browse files
Support binding to BinaryData (Azure#24659)
* Support binding to BinaryData * Add back accidentally removed testcase * PR FB
1 parent 1c0fb6f commit 064ee2d

File tree

3 files changed

+128
-38
lines changed

3 files changed

+128
-38
lines changed

sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/EventGridExtensionConfigProvider.cs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,14 @@ public void Initialize(ExtensionConfigContext context)
8080
.AddConverter<JToken, EventGridEvent[]>(jobject => EventGridEvent.ParseMany(new BinaryData(jobject.ToString())))
8181
.AddConverter<JToken, CloudEvent>(jobject => CloudEvent.Parse(new BinaryData(jobject.ToString())))
8282
.AddConverter<JToken, CloudEvent[]>(jobject => CloudEvent.ParseMany(new BinaryData(jobject.ToString())))
83+
.AddConverter<JToken, BinaryData>(jobject => new BinaryData(jobject.ToString()))
84+
.AddConverter<JToken, BinaryData[]>(jobject => jobject.Select(obj => new BinaryData(obj.ToString())).ToArray())
8385
.AddOpenConverter<JToken, OpenType.Poco>(typeof(JTokenToPocoConverter<>))
8486
.AddOpenConverter<JToken, OpenType.Poco[]>(typeof(JTokenToPocoConverter<>))
8587
.BindToTrigger<JToken>(new EventGridTriggerAttributeBindingProvider(this));
8688

8789
// Register the output binding
88-
var rule = context
89-
.AddBindingRule<EventGridAttribute>()
90-
//TODO - add binding for BinaryData?
91-
.AddConverter<string, object>(str =>
92-
{
93-
// first attempt to parse as EventGridEvent, then fallback to CloudEvent
94-
try
95-
{
96-
return EventGridEvent.Parse(new BinaryData(str));
97-
}
98-
catch (ArgumentException)
99-
{
100-
return CloudEvent.Parse(new BinaryData(str));
101-
}
102-
})
103-
.AddConverter<JObject, object>(jobject =>
104-
{
105-
try
106-
{
107-
return EventGridEvent.Parse(new BinaryData(jobject.ToString()));
108-
}
109-
catch (ArgumentException)
110-
{
111-
return CloudEvent.Parse(new BinaryData(jobject.ToString()));
112-
}
113-
});
114-
90+
var rule = context.AddBindingRule<EventGridAttribute>();
11591
rule.BindToCollector(_converter);
11692
rule.AddValidator((a, t) =>
11793
{

sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/OutputBinding/EventGridAsyncCollector.cs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
6565

6666
if (isEventGridEvent)
6767
{
68-
List<EventGridEvent> egEvents = new();
68+
List<EventGridEvent> egEvents = new(events.Count);
6969
foreach (string evt in events)
7070
{
7171
egEvents.Add(EventGridEvent.Parse(new BinaryData(evt)));
@@ -75,7 +75,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
7575
}
7676
else
7777
{
78-
List<CloudEvent> cloudEvents = new();
78+
List<CloudEvent> cloudEvents = new(events.Count);
7979
foreach (string evt in events)
8080
{
8181
cloudEvents.Add(CloudEvent.Parse(new BinaryData(evt)));
@@ -84,6 +84,39 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
8484
await _client.SendEventsAsync(cloudEvents, cancellationToken).ConfigureAwait(false);
8585
}
8686
}
87+
else if (firstEvent is BinaryData data)
88+
{
89+
bool isEventGridEvent = false;
90+
try
91+
{
92+
var ev = EventGridEvent.Parse(data);
93+
isEventGridEvent = true;
94+
}
95+
catch (ArgumentException)
96+
{
97+
}
98+
99+
if (isEventGridEvent)
100+
{
101+
List<EventGridEvent> egEvents = new(events.Count);
102+
foreach (BinaryData evt in events)
103+
{
104+
egEvents.Add(EventGridEvent.Parse(evt));
105+
}
106+
107+
await _client.SendEventsAsync(egEvents, cancellationToken).ConfigureAwait(false);
108+
}
109+
else
110+
{
111+
List<CloudEvent> cloudEvents = new(events.Count);
112+
foreach (BinaryData evt in events)
113+
{
114+
cloudEvents.Add(CloudEvent.Parse(evt));
115+
}
116+
117+
await _client.SendEventsAsync(cloudEvents, cancellationToken).ConfigureAwait(false);
118+
}
119+
}
87120
else if (firstEvent is JObject jObject)
88121
{
89122
bool isEventGridEvent = false;
@@ -98,7 +131,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
98131

99132
if (isEventGridEvent)
100133
{
101-
List<EventGridEvent> egEvents = new();
134+
List<EventGridEvent> egEvents = new(events.Count);
102135
foreach (JObject evt in events)
103136
{
104137
egEvents.Add(EventGridEvent.Parse(new BinaryData(evt.ToString())));
@@ -108,7 +141,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
108141
}
109142
else
110143
{
111-
List<CloudEvent> cloudEvents = new();
144+
List<CloudEvent> cloudEvents = new(events.Count);
112145
foreach (JObject evt in events)
113146
{
114147
cloudEvents.Add(CloudEvent.Parse(new BinaryData(evt.ToString())));
@@ -119,7 +152,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
119152
}
120153
else if (firstEvent is EventGridEvent)
121154
{
122-
List<EventGridEvent> egEvents = new();
155+
List<EventGridEvent> egEvents = new(events.Count);
123156
foreach (object evt in events)
124157
{
125158
egEvents.Add((EventGridEvent) evt);
@@ -128,7 +161,7 @@ public EventGridAsyncCollector(EventGridPublisherClient client)
128161
}
129162
else if (firstEvent is CloudEvent)
130163
{
131-
List<CloudEvent> cloudEvents = new();
164+
List<CloudEvent> cloudEvents = new(events.Count);
132165
foreach (object evt in events)
133166
{
134167
cloudEvents.Add((CloudEvent) evt);

sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/tests/JobhostEndToEnd.cs

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class JobhostEndToEnd
2828

2929
[Theory]
3030
[TestCase("EventGridParams.TestEventGridToString_Single")]
31+
[TestCase("EventGridParams.TestEventGridToBinaryData_Single")]
3132
[TestCase("EventGridParams.TestEventGridToJObject_Single")]
3233
[TestCase("EventGridParams.TestEventGridToNuget_Single")]
3334
[TestCase("EventGridParams.TestEventGridToValidCustom_Single")]
@@ -52,6 +53,7 @@ public async Task ConsumeEventGridEventTest_Single(string functionName)
5253

5354
[Theory]
5455
[TestCase("CloudEventParams.TestEventGridToString_Single")]
56+
[TestCase("CloudEventParams.TestEventGridToBinaryData_Single")]
5557
[TestCase("CloudEventParams.TestEventGridToJObject_Single")]
5658
[TestCase("CloudEventParams.TestEventGridToNuget_Single")]
5759
[TestCase("CloudEventParams.TestEventGridToValidCustom_Single")]
@@ -74,6 +76,7 @@ public async Task ConsumeCloudEventTest_Single(string functionName)
7476
[Theory]
7577
[TestCase("EventGridParams.TestEventGridToCollection_Batch")]
7678
[TestCase("EventGridParams.TestEventGridToStringCollection_Batch")]
79+
[TestCase("EventGridParams.TestEventGridToBinaryDataCollection_Batch")]
7780
[TestCase("EventGridParams.TestEventGridToJObjectCollection_Batch")]
7881
[TestCase("EventGridParams.TestEventGridToCustomCollection_Batch")]
7982
public async Task ConsumeEventGridEventTests_Batch(string functionName)
@@ -96,6 +99,7 @@ public async Task ConsumeEventGridEventTests_Batch(string functionName)
9699
[Theory]
97100
[TestCase("CloudEventParams.TestEventGridToCollection_Batch")]
98101
[TestCase("CloudEventParams.TestEventGridToStringCollection_Batch")]
102+
[TestCase("CloudEventParams.TestEventGridToBinaryDataCollection_Batch")]
99103
[TestCase("CloudEventParams.TestEventGridToJObjectCollection_Batch")]
100104
[TestCase("CloudEventParams.TestEventGridToCustomCollection_Batch")]
101105
public async Task ConsumeCloudEventTests_Batch(string functionName)
@@ -223,13 +227,15 @@ public void OutputBindingInvalidCredentialTests()
223227
[Theory]
224228
[TestCase("SingleEvent", "0")]
225229
[TestCase("SingleEventString", "0")]
230+
[TestCase("SingleEventBinaryData", "0")]
226231
[TestCase("SingleEventJObject", "0")]
227232
[TestCase("SingleReturnEvent", "0")]
228233
// space separated string as event ids
229234
[TestCase("ArrayEvent", "0 1 2 3 4")]
230235
[TestCase("CollectorEvent", "0 1 2 3")]
231236
[TestCase("AsyncCollectorEvent", "0 1 2 3 4 5 6")]
232237
[TestCase("StringEvents", "0 1 2 3 4")]
238+
[TestCase("BinaryDataEvents", "0 1 2 3 4")]
233239
[TestCase("JObjectEvents", "0 1 2 3 4")]
234240
public async Task OutputBindingParamsTests(string functionName, string expectedCollection)
235241
{
@@ -283,11 +289,21 @@ public void TestEventGridToString_Single([EventGridTrigger] string value)
283289
_functionOut = (string)JObject.Parse(value)["subject"];
284290
}
285291

292+
public void TestEventGridToBinaryData_Single([EventGridTrigger] BinaryData value)
293+
{
294+
_functionOut = (string)JObject.Parse(value.ToString())["subject"];
295+
}
296+
286297
public void TestEventGridToStringCollection_Batch([EventGridTrigger] string[] values)
287298
{
288299
_functionOut = string.Join(", ", values.Select(v => (string)JObject.Parse(v)["subject"]));
289300
}
290301

302+
public void TestEventGridToBinaryDataCollection_Batch([EventGridTrigger] BinaryData[] values)
303+
{
304+
_functionOut = string.Join(", ", values.Select(v => (string)JObject.Parse(v.ToString())["subject"]));
305+
}
306+
291307
public void TestEventGridToJObject_Single([EventGridTrigger] JObject value)
292308
{
293309
_functionOut = (string)value["subject"];
@@ -328,13 +344,15 @@ public void TestEventGridToCustomCollection_Batch([EventGridTrigger] ValidPoco[]
328344
[Theory]
329345
[TestCase("SingleEvent", "0")]
330346
[TestCase("SingleEventString", "0")]
347+
[TestCase("SingleEventBinaryData", "0")]
331348
[TestCase("SingleEventJObject", "0")]
332349
[TestCase("SingleReturnEvent", "0")]
333350
// space separated string as event ids
334351
[TestCase("ArrayEvent", "0 1 2 3 4")]
335352
[TestCase("CollectorEvent", "0 1 2 3")]
336353
[TestCase("AsyncCollectorEvent", "0 1 2 3 4 5 6")]
337354
[TestCase("StringEvents", "0 1 2 3 4")]
355+
[TestCase("BinaryDataEvents", "0 1 2 3 4")]
338356
[TestCase("JObjectEvents", "0 1 2 3 4")]
339357
public async Task OutputCloudEventBindingParamsTests(string functionName, string expectedCollection)
340358
{
@@ -419,11 +437,21 @@ public void TestEventGridToString_Single([EventGridTrigger] string value)
419437
_functionOut = (string)JObject.Parse(value)["subject"];
420438
}
421439

440+
public void TestEventGridToBinaryData_Single([EventGridTrigger] BinaryData value)
441+
{
442+
_functionOut = (string)JObject.Parse(value.ToString())["subject"];
443+
}
444+
422445
public void TestEventGridToStringCollection_Batch([EventGridTrigger] string[] values)
423446
{
424447
_functionOut = string.Join(", ", values.Select(v => (string)JObject.Parse(v)["subject"]));
425448
}
426449

450+
public void TestEventGridToBinaryDataCollection_Batch([EventGridTrigger] BinaryData[] values)
451+
{
452+
_functionOut = string.Join(", ", values.Select(v => (string)JObject.Parse(v.ToString())["subject"]));
453+
}
454+
427455
public void TestEventGridToJObject_Single([EventGridTrigger] JObject value)
428456
{
429457
_functionOut = (string)value["subject"];
@@ -500,9 +528,9 @@ public void TestString(
500528

501529
public void TestDataFieldMissing(
502530
[EventGridTrigger] JObject value,
503-
[BindingData("{data}")] string autoResovle)
531+
[BindingData("{data}")] string autoResolve)
504532
{
505-
_functionOut = autoResovle;
533+
_functionOut = autoResolve;
506534
}
507535

508536
// auto resolve only works for string
@@ -566,6 +594,18 @@ public void SingleEventString([EventGrid(TopicEndpointUri = "eventgridUri", Topi
566594
}";
567595
}
568596

597+
public void SingleEventBinaryData([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out BinaryData single)
598+
{
599+
single = new BinaryData(@"
600+
{
601+
""id"" : ""id"",
602+
""data"" : ""0"",
603+
""eventType"" : ""custom"",
604+
""subject"" : ""custom"",
605+
""dataVersion"" : ""1""
606+
}");
607+
}
608+
569609
public void SingleEventJObject([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out JObject single)
570610
{
571611
single = new JObject(
@@ -625,7 +665,6 @@ public async Task AsyncCollectorEvent([EventGrid(TopicEndpointUri = "eventgridUr
625665
}
626666
}
627667

628-
// assume converter is applied correctly with other output binding types
629668
public void StringEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out string[] strings)
630669
{
631670
strings = new string[5];
@@ -642,7 +681,22 @@ public void StringEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeyS
642681
}
643682
}
644683

645-
// assume converter is applied correctly with other output binding types
684+
public void BinaryDataEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out BinaryData[] data)
685+
{
686+
data = new BinaryData[5];
687+
for (int i = 0; i < 5; i++)
688+
{
689+
data[i] = new BinaryData($@"
690+
{{
691+
""id"" : ""{i}"",
692+
""data"" : ""{i}"",
693+
""eventType"" : ""custom"",
694+
""subject"" : ""custom"",
695+
""dataVersion"" : ""1""
696+
}}");
697+
}
698+
}
699+
646700
public void JObjectEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out JObject[] jobjects)
647701
{
648702
jobjects = new JObject[5];
@@ -683,6 +737,18 @@ public void SingleEventString([EventGrid(TopicEndpointUri = "eventgridUri", Topi
683737
}";
684738
}
685739

740+
public void SingleEventBinaryData([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out BinaryData single)
741+
{
742+
single = new BinaryData(@"
743+
{
744+
""id"" : ""i"",
745+
""data"" : ""0"",
746+
""source"" : ""custom"",
747+
""type"" : ""custom"",
748+
""specversion"" : ""1.0""
749+
}");
750+
}
751+
686752
public void SingleEventJObject([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out JObject single)
687753
{
688754
single = new JObject(
@@ -730,7 +796,6 @@ public async Task AsyncCollectorEvent([EventGrid(TopicEndpointUri = "eventgridUr
730796
}
731797
}
732798

733-
// assume converter is applied correctly with other output binding types
734799
public void StringEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out string[] strings)
735800
{
736801
strings = new string[5];
@@ -747,6 +812,22 @@ public void StringEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeyS
747812
}
748813
}
749814

815+
public void BinaryDataEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out BinaryData[] data)
816+
{
817+
data = new BinaryData[5];
818+
for (int i = 0; i < 5; i++)
819+
{
820+
data[i] = new BinaryData($@"
821+
{{
822+
""id"" : ""{i}"",
823+
""data"" : ""{i}"",
824+
""source"" : ""custom"",
825+
""type"" : ""custom"",
826+
""specversion"" : ""1.0""
827+
}}");
828+
}
829+
}
830+
750831
// assume converter is applied correctly with other output binding types
751832
public void JObjectEvents([EventGrid(TopicEndpointUri = "eventgridUri", TopicKeySetting = "eventgridKey")] out JObject[] jobjects)
752833
{

0 commit comments

Comments
 (0)