Skip to content

Commit 535da92

Browse files
matzewduglin
authored andcommitted
Adding a simple dataref extension, similar to the java sdk
Signed-off-by: Matthias Wessendorf <[email protected]> Signed-off-by: Doug Davis <[email protected]>
1 parent 74ac76d commit 535da92

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Diff for: v2/extensions/dataref_extension.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2024 The CloudEvents Authors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package extensions
7+
8+
import (
9+
"github.com/cloudevents/sdk-go/v2/event"
10+
"net/url"
11+
)
12+
13+
const DataRefExtensionKey = "dataref"
14+
15+
// DataRefExtension represents the CloudEvents Dataref (claim check pattern)
16+
// extension for cloudevents contexts,
17+
// See https://github.com/cloudevents/spec/blob/main/cloudevents/extensions/dataref.md
18+
// for more info
19+
type DataRefExtension struct {
20+
DataRef string `json:"dataref"`
21+
}
22+
23+
// AddDataRefExtension adds the dataref attribute to the cloudevents context
24+
func AddDataRefExtension(e *event.Event, dataRef string) error {
25+
if _, err := url.Parse(dataRef); err != nil {
26+
return err
27+
}
28+
e.SetExtension(DataRefExtensionKey, dataRef)
29+
return nil
30+
}
31+
32+
// GetDataRefExtension returns any dataref attribute present in the
33+
// cloudevent event/context and a bool to indicate if it was found.
34+
// If not found, the DataRefExtension.DataRef value will be ""
35+
func GetDataRefExtension(e event.Event) (DataRefExtension, bool) {
36+
if dataRefValue, ok := e.Extensions()[DataRefExtensionKey]; ok {
37+
dataRefStr, _ := dataRefValue.(string)
38+
return DataRefExtension{DataRef: dataRefStr}, true
39+
}
40+
return DataRefExtension{}, false
41+
}

Diff for: v2/extensions/dataref_extension_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2024 The CloudEvents Authors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package extensions
7+
8+
import (
9+
"testing"
10+
11+
"github.com/cloudevents/sdk-go/v2/event"
12+
)
13+
14+
func TestAddDataRefExtension(t *testing.T) {
15+
// MUST HAVE AT LEAST ONE FAILING TEST as that'll also test to make sure
16+
// that the failed add() call won't actually set anything
17+
18+
tests := []struct {
19+
dataref string
20+
pass bool
21+
}{
22+
{"https://example.com/data", true},
23+
{"://invalid-url", false},
24+
}
25+
26+
for _, test := range tests {
27+
e := event.New()
28+
err := AddDataRefExtension(&e, test.dataref)
29+
30+
// Make sure adding it passed/fails appropriately
31+
if test.pass && err != nil {
32+
t.Fatalf("Failed to add DataRefExtension with valid URL(%s): %s",
33+
test.dataref, err)
34+
}
35+
if !test.pass && err == nil {
36+
t.Fatalf("Expected not to find DataRefExtension (%s), but did",
37+
test.dataref)
38+
}
39+
40+
// Now make sure it's actually there in the 'pass' cases, but
41+
// missing in the failed cases.
42+
dr, ok := GetDataRefExtension(e)
43+
if test.pass {
44+
if !ok || dr.DataRef == "" {
45+
t.Fatalf("Dataref (%s) is missing after being set",
46+
test.dataref)
47+
}
48+
if dr.DataRef != test.dataref {
49+
t.Fatalf("Retrieved dataref(%v) doesn't match set value(%s)",
50+
dr, test.dataref)
51+
}
52+
} else {
53+
if ok || dr.DataRef != "" {
54+
t.Fatalf("Expected not to find DataRefExtension, but did(%s)",
55+
test.dataref)
56+
}
57+
}
58+
}
59+
}
60+
61+
func TestGetDataRefExtensionNotFound(t *testing.T) {
62+
e := event.New()
63+
64+
// Make sure there's no dataref by default
65+
dr, ok := GetDataRefExtension(e)
66+
if ok || dr.DataRef != "" {
67+
t.Fatal("Expected not to find DataRefExtension, but did")
68+
}
69+
}

0 commit comments

Comments
 (0)