@@ -151,56 +151,23 @@ func (a *api) getInterestedTopics() (map[string]TopicSubscriptions, error) {
151
151
}
152
152
153
153
func (a * api ) publishMessageGRPC (ctx context.Context , msg * pubsub.NewMessage ) error {
154
- // 1. Unmarshal to cloudEvent model
155
- var cloudEvent map [string ]interface {}
156
- err := a .json .Unmarshal (msg .Data , & cloudEvent )
154
+
155
+ // TODO tracing
156
+ envelope , cloudEvent , err := a .envelopeFromSubscriptionMessage (ctx , msg )
157
+
157
158
if err != nil {
158
- log .DefaultLogger .Debugf ("[runtime]error deserializing cloud events proto: %s" , err )
159
159
return err
160
160
}
161
161
162
- // 2. Drop msg if the current cloud event has expired
163
- if pubsub .HasExpired (cloudEvent ) {
164
- log .DefaultLogger .Warnf ("[runtime]dropping expired pub/sub event %v as of %v" , cloudEvent [pubsub .IDField ].(string ), cloudEvent [pubsub .ExpirationField ].(string ))
162
+ if envelope == nil {
165
163
return nil
166
164
}
167
165
168
- // 3. Convert to proto domain struct
169
- envelope := & runtimev1pb.TopicEventRequest {
170
- Id : cloudEvent [pubsub .IDField ].(string ),
171
- Source : cloudEvent [pubsub .SourceField ].(string ),
172
- DataContentType : cloudEvent [pubsub .DataContentTypeField ].(string ),
173
- Type : cloudEvent [pubsub .TypeField ].(string ),
174
- SpecVersion : cloudEvent [pubsub .SpecVersionField ].(string ),
175
- Topic : msg .Topic ,
176
- PubsubName : msg .Metadata [Metadata_key_pubsubName ],
177
- }
178
-
179
- // set data field
180
- if data , ok := cloudEvent [pubsub .DataBase64Field ]; ok && data != nil {
181
- decoded , decodeErr := base64 .StdEncoding .DecodeString (data .(string ))
182
- if decodeErr != nil {
183
- log .DefaultLogger .Debugf ("unable to base64 decode cloudEvent field data_base64: %s" , decodeErr )
184
- return err
185
- }
186
-
187
- envelope .Data = decoded
188
- } else if data , ok := cloudEvent [pubsub .DataField ]; ok && data != nil {
189
- envelope .Data = nil
190
-
191
- if contenttype .IsStringContentType (envelope .DataContentType ) {
192
- envelope .Data = []byte (data .(string ))
193
- } else if contenttype .IsJSONContentType (envelope .DataContentType ) {
194
- envelope .Data , _ = a .json .Marshal (data )
195
- }
196
- }
197
- // TODO tracing
198
-
199
- // 4. Call appcallback
166
+ // Call appcallback
200
167
clientV1 := runtimev1pb .NewAppCallbackClient (a .AppCallbackConn )
201
168
res , err := clientV1 .OnTopicEvent (ctx , envelope )
202
169
203
- // 5. Check result
170
+ // Check result
204
171
return retryStrategy (err , res , cloudEvent )
205
172
}
206
173
@@ -246,3 +213,50 @@ func listTopicSubscriptions(client runtimev1pb.AppCallbackClient, log log.ErrorL
246
213
}
247
214
return make ([]* runtimev1pb.TopicSubscription , 0 )
248
215
}
216
+
217
+ func (a * api ) envelopeFromSubscriptionMessage (ctx context.Context , msg * pubsub.NewMessage ) (* runtimev1pb.TopicEventRequest , map [string ]interface {}, error ) {
218
+ // 1. Unmarshal to cloudEvent model
219
+ var cloudEvent map [string ]interface {}
220
+ err := a .json .Unmarshal (msg .Data , & cloudEvent )
221
+ if err != nil {
222
+ log .DefaultLogger .Debugf ("[runtime]error deserializing cloud events proto: %s" , err )
223
+ return nil , cloudEvent , err
224
+ }
225
+
226
+ // 2. Drop msg if the current cloud event has expired
227
+ if pubsub .HasExpired (cloudEvent ) {
228
+ log .DefaultLogger .Warnf ("[runtime]dropping expired pub/sub event %v as of %v" , cloudEvent [pubsub .IDField ].(string ), cloudEvent [pubsub .ExpirationField ].(string ))
229
+ return nil , cloudEvent , nil
230
+ }
231
+
232
+ // 3. Convert to proto domain struct
233
+ envelope := & runtimev1pb.TopicEventRequest {
234
+ Id : cloudEvent [pubsub .IDField ].(string ),
235
+ Source : cloudEvent [pubsub .SourceField ].(string ),
236
+ DataContentType : cloudEvent [pubsub .DataContentTypeField ].(string ),
237
+ Type : cloudEvent [pubsub .TypeField ].(string ),
238
+ SpecVersion : cloudEvent [pubsub .SpecVersionField ].(string ),
239
+ Topic : msg .Topic ,
240
+ PubsubName : msg .Metadata [Metadata_key_pubsubName ],
241
+ }
242
+
243
+ // set data field
244
+ if data , ok := cloudEvent [pubsub .DataBase64Field ]; ok && data != nil {
245
+ decoded , decodeErr := base64 .StdEncoding .DecodeString (data .(string ))
246
+ if decodeErr != nil {
247
+ log .DefaultLogger .Debugf ("unable to base64 decode cloudEvent field data_base64: %s" , decodeErr )
248
+ return nil , cloudEvent , err
249
+ }
250
+
251
+ envelope .Data = decoded
252
+ } else if data , ok := cloudEvent [pubsub .DataField ]; ok && data != nil {
253
+ envelope .Data = nil
254
+
255
+ if contenttype .IsStringContentType (envelope .DataContentType ) {
256
+ envelope .Data = []byte (data .(string ))
257
+ } else if contenttype .IsJSONContentType (envelope .DataContentType ) {
258
+ envelope .Data , _ = a .json .Marshal (data )
259
+ }
260
+ }
261
+ return envelope , cloudEvent , nil
262
+ }
0 commit comments