-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathmetadata.go
More file actions
41 lines (37 loc) · 1.02 KB
/
metadata.go
File metadata and controls
41 lines (37 loc) · 1.02 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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package binding
import (
"github.com/cloudevents/sdk-go/v2/types"
)
// ExtractMetadata reads metadata extensions from a MessageMetadataReader and maps them to the target pointers.
// It skips empty values to ensure only valid data is written.
func ExtractMetadata[T ~string](reader MessageMetadataReader, mapping map[string]*T) error {
for name, target := range mapping {
v := reader.GetExtension(name)
if v == nil {
continue
}
s, err := types.Format(v)
if err != nil {
return err
}
*target = T(s)
}
return nil
}
// AttachMetadata sets metadata extensions on a MessageMetadataWriter using the provided mapping.
// It skips empty values to ensure only valid data is written.
func AttachMetadata[T ~string](writer MessageMetadataWriter, mapping map[string]T) error {
for name, value := range mapping {
if value == "" {
continue
}
if err := writer.SetExtension(name, value); err != nil {
return err
}
}
return nil
}