-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
60 lines (56 loc) · 2.36 KB
/
context.go
File metadata and controls
60 lines (56 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package mongodb
import (
"context"
"encoding/json"
"log/slog"
event "github.com/rbaliyan/event/v3"
)
// Metadata keys for event context.
const (
// MetadataContentType is the MIME type of the message payload (e.g. "application/json").
MetadataContentType = "Content-Type"
// MetadataOperation is the MongoDB change stream operation type (insert, update, replace, delete).
MetadataOperation = "operation"
// MetadataDatabase is the name of the MongoDB database where the change occurred.
MetadataDatabase = "database"
// MetadataCollection is the name of the MongoDB collection where the change occurred.
MetadataCollection = "collection"
// MetadataNamespace is the fully qualified namespace in "database.collection" format.
MetadataNamespace = "namespace"
// MetadataDocumentKey is the string representation of the changed document's _id field.
MetadataDocumentKey = "document_key"
// MetadataClusterTime is the MongoDB cluster time of the change event in RFC3339Nano format.
MetadataClusterTime = "cluster_time"
// MetadataUpdatedFields is the JSON-encoded map of fields updated by an update operation.
// Set only when WithUpdateDescription() is enabled on the transport.
MetadataUpdatedFields = "updated_fields"
// MetadataRemovedFields is the JSON-encoded array of field names removed by an update operation.
// Set only when WithUpdateDescription() is enabled on the transport.
MetadataRemovedFields = "removed_fields"
)
// ContextUpdateDescription extracts UpdateDescription from event context metadata.
// Returns nil if metadata doesn't contain update description fields.
// Requires WithUpdateDescription() to be set on the transport.
func ContextUpdateDescription(ctx context.Context) *UpdateDescription {
md := event.ContextMetadata(ctx)
if md == nil {
return nil
}
updated, hasUpdated := md[MetadataUpdatedFields]
removed, hasRemoved := md[MetadataRemovedFields]
if !hasUpdated && !hasRemoved {
return nil
}
desc := &UpdateDescription{}
if hasUpdated && updated != "" {
if err := json.Unmarshal([]byte(updated), &desc.UpdatedFields); err != nil {
slog.DebugContext(ctx, "failed to unmarshal updated_fields metadata", "error", err)
}
}
if hasRemoved && removed != "" {
if err := json.Unmarshal([]byte(removed), &desc.RemovedFields); err != nil {
slog.DebugContext(ctx, "failed to unmarshal removed_fields metadata", "error", err)
}
}
return desc
}