in funcframework/events.go [108:156]
func getBackgroundEvent(body []byte, path string) (*metadata.Metadata, interface{}, error) {
// Known background event types that the incoming request could represent.
// Event types are mutually exclusive. During unmarshalling, only the field
// for the matching type is populated.
type possibleEvents struct {
*pubsub.LegacyPushSubscriptionEvent
*fftypes.BackgroundEvent
}
// Attempt to unmarshal into one of the known background event types.
possible := possibleEvents{}
if err := json.Unmarshal(body, &possible); err != nil {
return nil, nil, err
}
event := possible.BackgroundEvent
// If the background event payload is missing, check if it's a legacy
// Pub/Sub event.
if possible.BackgroundEvent == nil && possible.LegacyPushSubscriptionEvent != nil {
topic, err := pubsub.ExtractTopicFromRequestPath(path)
if err != nil {
fmt.Printf("WARNING: %s", err)
}
event = possible.LegacyPushSubscriptionEvent.ToBackgroundEvent(topic)
}
// If there is no "data" payload, this isn't a background event, but that's okay.
if event == nil || event.Data == nil {
return nil, nil, nil
}
// If the "context" field was present, we have a complete event and so return.
if event.Metadata != nil {
return event.Metadata, event.Data, nil
}
// Otherwise, try to directly populate a metadata object.
m := &metadata.Metadata{}
if err := json.Unmarshal(body, m); err != nil {
return nil, nil, err
}
// Check for event ID to see if this is a background event, but if not that's okay.
if m.EventID == "" {
return nil, nil, nil
}
return m, event.Data, nil
}