@@ -321,58 +321,20 @@ func (in topicEventJSON) getData() (data any, rawData []byte) {
321
321
return data , rawData
322
322
}
323
323
324
- func (in BulkTopicJson ) getData () (data any , rawData []byte ) {
325
- var (
326
- err error
327
- v any
328
- )
329
- if len (in .Data ) > 0 {
330
- rawData = []byte (in .Data )
331
- data = rawData
332
- // We can assume that rawData is valid JSON
333
- // without checking in.DataContentType == "application/json".
334
- if err = json .Unmarshal (rawData , & v ); err == nil {
335
- data = v
336
- // Handling of JSON base64 encoded or escaped in a string.
337
- if str , ok := v .(string ); ok {
338
- // This is the path that will most likely succeed.
339
- var (
340
- vString any
341
- decoded []byte
342
- )
343
- if err = json .Unmarshal ([]byte (str ), & vString ); err == nil {
344
- data = vString
345
- } else if decoded , err = base64 .StdEncoding .DecodeString (str ); err == nil {
346
- // Decoded Base64 encoded JSON does not seem to be in the spec
347
- // but it is in existing unit tests so this handles that case.
348
- var vBase64 any
349
- if err = json .Unmarshal (decoded , & vBase64 ); err == nil {
350
- data = vBase64
351
- }
352
- }
353
- }
354
- }
355
- }
356
-
357
- return data , rawData
324
+ type BulkSubscribeMessageItem struct {
325
+ EntryId string `json:"entryId"` //nolint:stylecheck
326
+ Event interface {} `json:"event"`
327
+ Metadata map [string ]string `json:"metadata"`
328
+ ContentType string `json:"contentType,omitempty"`
358
329
}
359
330
360
- type BulkTopicJson struct {
361
- ContentType string `json:"contentType"`
362
- EntryID string `json:"entryId"`
363
- Event map [string ]string `json:"event"`
364
- Data json.RawMessage `json:"data"`
365
- DataContentType string `json:"datacontenttype"`
366
- ID string `json:"id"`
367
- PubsubName string `json:"pubsubname"`
368
- Source string `json:"source"`
369
- SpecVersion string `json:"specversion"`
370
- Time string `json:"time"`
371
- Topic string `json:"topic"`
372
- TraceID string `json:"traceid"`
373
- TraceParent string `json:"traceparent"`
374
- TraceState string `json:"tracestate"`
375
- Metadata interface {} `json:"metadata"`
331
+ type BulkSubscribeEnvelope struct {
332
+ ID string
333
+ Entries []BulkSubscribeMessageItem
334
+ Metadata map [string ]string
335
+ Topic string
336
+ Pubsub string
337
+ EventType string
376
338
}
377
339
378
340
// == APP == the item is {application/cloudevents+json 5e582fd2-f1c4-47bd-81b1-803fb7e86552 map[data:multi-pong datacontenttype:text/plain id:92fc5348-097d-4b9f-b093-7e6fcda77add pubsubname:messages source:pub specversion:1.0 time:2023-12-02T11:42:31+05:30 topic:neworder traceid:00-a0373ef078e14e8db358c06e0ec18b27-d8dae6b5080eb9da-01 traceparent:00-a0373ef078e14e8db358c06e0ec18b27-d8dae6b5080eb9da-01 tracestate: type:com.dapr.event.sent] [] <nil>}
@@ -410,88 +372,70 @@ func (s *Server) AddBulkTopicEventHandler(sub *common.Subscription, fn common.Bu
410
372
}
411
373
412
374
// deserialize the event
413
- var ins map [ string ] interface {}
375
+ var ins BulkSubscribeEnvelope
414
376
if err = json .Unmarshal (body , & ins ); err != nil {
415
377
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
416
378
return
417
379
}
418
380
419
- entriesInterface , ok := ins ["entries" ].([]interface {})
420
- if ! ok {
421
- // Handle the error or return an error response
422
- http .Error (w , "Entries format error" , PubSubHandlerDropStatusCode )
423
- return
424
- }
425
-
426
- statuses := make ([]BulkSubscribeResponseEntry , 0 , len (entriesInterface ))
427
-
428
- var messages []common.BulkTopic
429
- for _ , entry := range entriesInterface {
430
- itemMap , ok := entry .(map [string ]interface {})
431
- if ! ok {
432
- http .Error (w , "Entry format error" , PubSubHandlerDropStatusCode )
433
- return
434
- }
381
+ statuses := make ([]BulkSubscribeResponseEntry , 0 , len (ins .Entries ))
435
382
436
- itemJSON , err := json .Marshal (itemMap ["event" ])
383
+ var messages []common.TopicEvent
384
+ for _ , entry := range ins .Entries {
385
+ itemJSON , err := json .Marshal (entry .Event )
437
386
if err != nil {
438
387
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
439
388
return
440
389
}
441
- var item BulkTopicJson
390
+ var in topicEventJSON
442
391
443
- if err := json .Unmarshal (itemJSON , & item ); err != nil {
392
+ if err := json .Unmarshal (itemJSON , & in ); err != nil {
444
393
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
445
394
return
446
395
}
447
- data , rawData := item .getData ()
448
-
449
-
450
- if item .PubsubName == "" {
451
- item .PubsubName = sub .PubsubName
396
+ if in .PubsubName == "" {
397
+ in .Topic = sub .PubsubName
452
398
}
453
-
454
- if item .Topic == "" {
455
- item .Topic = sub .Topic
399
+ if in .Topic == "" {
400
+ in .Topic = sub .Topic
456
401
}
402
+ data , rawData := in .getData ()
457
403
458
404
statuses = append (statuses , BulkSubscribeResponseEntry {
459
- entryId : item . EntryID ,
405
+ entryId : in . ID ,
460
406
status : SubscriptionResponseStatusSuccess ,
461
407
},
462
408
)
463
409
464
- newItem := common.BulkTopic {
465
- ContentType : item .ContentType ,
466
- EntryID : item .EntryID ,
467
- Event : item .Event ,
410
+ te := common.TopicEvent {
411
+ ID : in .ID ,
412
+ SpecVersion : in .SpecVersion ,
413
+ Type : in .Type ,
414
+ Source : in .Source ,
415
+ DataContentType : in .DataContentType ,
468
416
Data : data ,
469
417
RawData : rawData ,
470
- DataContentType : item .DataContentType ,
471
- ID : item .EntryID ,
472
- PubsubName : item .PubsubName ,
473
- Source : item .Source ,
474
- SpecVersion : item .SpecVersion ,
475
- Metadata : item .Metadata ,
476
- Time : item .Time ,
477
- Topic : item .Topic ,
478
- TraceID : item .TraceID ,
479
- TraceParent : item .TraceParent ,
480
- TraceState : item .TraceState ,
418
+ DataBase64 : in .DataBase64 ,
419
+ Subject : in .Subject ,
420
+ PubsubName : in .PubsubName ,
421
+ Topic : in .Topic ,
481
422
}
482
423
483
- messages = append (messages , newItem )
424
+ messages = append (messages , te )
484
425
}
485
426
resp := BulkSubscribeResponse {
486
427
statuses : statuses ,
487
428
}
488
- responseJSON , err := json .Marshal (resp )
489
429
if err != nil {
490
430
http .Error (w , err .Error (), PubSubHandlerDropStatusCode )
491
431
return
492
432
}
493
433
w .Header ().Add ("Content-Type" , "application/json" )
494
- w .Write (responseJSON )
434
+ if err := json .NewEncoder (w ).Encode (resp ); err != nil {
435
+ http .Error (w , err .Error (), http .StatusInternalServerError )
436
+ return
437
+ }
438
+ w .WriteHeader (http .StatusOK )
495
439
496
440
retry , err := fn (r .Context (), messages )
497
441
if err == nil {
0 commit comments