Skip to content

Commit 1c45f4e

Browse files
committed
minor changes
Signed-off-by: sadath-12 <[email protected]>
1 parent 3f5bcd8 commit 1c45f4e

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

service/common/type.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ type BulkTopic struct {
5151
ContentType string `json:"contentType"`
5252
EntryID string `json:"entryId"`
5353
Event map[string]string `json:"event"`
54-
Data json.RawMessage `json:"data"`
54+
RawData []byte `json:"-"`
55+
Data interface{} `json:"data"`
5556
DataContentType string `json:"datacontenttype"`
5657
ID string `json:"id"`
5758
PubsubName string `json:"pubsubname"`

service/http/topic.go

+49-19
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@ type topicEventJSON struct {
6666
Topic string `json:"topic"`
6767
// PubsubName is name of the pub/sub this message came from
6868
PubsubName string `json:"pubsubname"`
69-
// EntryID is the unique identifier of the entry
70-
EntryID string `json:"entryId"`
71-
// Event is a map of strings that contains additional information about the event
72-
Event map[string]string `json:"event"`
73-
// Time is the timestamp of the event
74-
Time string `json:"time"`
75-
// TraceID is the identifier of the trace
76-
TraceID string `json:"traceid"`
77-
// TraceParent is the identifier of the parent span
78-
TraceParent string `json:"traceparent"`
79-
// TraceState is the state of the trace
80-
TraceState string `json:"tracestate"`
81-
// Metadata is an interface that contains any other metadata associated with the event
82-
Metadata interface{} `json:"metadata"`
8369
}
8470

8571
func (s *Server) registerBaseHandler() {
@@ -270,8 +256,6 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn common.TopicE
270256
return nil
271257
}
272258

273-
274-
275259
func (in topicEventJSON) getData() (data any, rawData []byte) {
276260
var (
277261
err error
@@ -318,6 +302,42 @@ func (in topicEventJSON) getData() (data any, rawData []byte) {
318302
return data, rawData
319303
}
320304

305+
func (in BulkTopicJson) getData() (data any, rawData []byte) {
306+
var (
307+
err error
308+
v any
309+
)
310+
if len(in.Data) > 0 {
311+
rawData = []byte(in.Data)
312+
data = rawData
313+
// We can assume that rawData is valid JSON
314+
// without checking in.DataContentType == "application/json".
315+
if err = json.Unmarshal(rawData, &v); err == nil {
316+
data = v
317+
// Handling of JSON base64 encoded or escaped in a string.
318+
if str, ok := v.(string); ok {
319+
// This is the path that will most likely succeed.
320+
var (
321+
vString any
322+
decoded []byte
323+
)
324+
if err = json.Unmarshal([]byte(str), &vString); err == nil {
325+
data = vString
326+
} else if decoded, err = base64.StdEncoding.DecodeString(str); err == nil {
327+
// Decoded Base64 encoded JSON does not seem to be in the spec
328+
// but it is in existing unit tests so this handles that case.
329+
var vBase64 any
330+
if err = json.Unmarshal(decoded, &vBase64); err == nil {
331+
data = vBase64
332+
}
333+
}
334+
}
335+
}
336+
}
337+
338+
return data, rawData
339+
}
340+
321341
type BulkTopicJson struct {
322342
ContentType string `json:"contentType"`
323343
EntryID string `json:"entryId"`
@@ -404,14 +424,25 @@ func (s *Server) AddBulkTopicEventHandler(sub *common.Subscription, fn common.Bu
404424
return
405425
}
406426

427+
data, rawData := item.getData()
428+
429+
if item.PubsubName == "" {
430+
item.PubsubName = sub.PubsubName
431+
}
432+
433+
if item.Topic == "" {
434+
item.Topic = sub.Topic
435+
}
436+
407437

408438
newItem := common.BulkTopic{
409439
ContentType: item.ContentType,
410440
EntryID: item.EntryID,
411441
Event: item.Event,
412-
Data: item.Data,
442+
Data: data,
443+
RawData: rawData,
413444
DataContentType: item.DataContentType,
414-
ID: item.ID,
445+
ID: item.EntryID,
415446
PubsubName: item.PubsubName,
416447
Source: item.Source,
417448
SpecVersion: item.SpecVersion,
@@ -447,7 +478,6 @@ func (s *Server) AddBulkTopicEventHandler(sub *common.Subscription, fn common.Bu
447478
return nil
448479
}
449480

450-
451481
func writeStatus(w http.ResponseWriter, s string) {
452482
status := &common.SubscriptionResponse{Status: s}
453483
if err := json.NewEncoder(w).Encode(status); err != nil {

0 commit comments

Comments
 (0)