Skip to content

Commit 9c287f1

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 9c287f1

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

v2/extensions/dataref_extension.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
type DataRefExtension struct {
16+
DataRef string `json:"dataref"`
17+
}
18+
19+
func AddDataRefExtension(e *event.Event, dataRef string) error {
20+
if _, err := url.Parse(dataRef); err != nil {
21+
return err
22+
}
23+
e.SetExtension(DataRefExtensionKey, dataRef)
24+
return nil
25+
}
26+
27+
func GetDataRefExtension(e event.Event) (*DataRefExtension, bool) {
28+
if dataRefValue, ok := e.Extensions()[DataRefExtensionKey]; ok {
29+
dataRefStr, ok := dataRefValue.(string)
30+
if !ok {
31+
return nil, false
32+
}
33+
return &DataRefExtension{DataRef: dataRefStr}, true
34+
}
35+
return nil, false
36+
}
Lines changed: 69 additions & 0 deletions
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 {
45+
t.Fatalf("Dataref (%s) is missing after being set",
46+
test.dataref)
47+
}
48+
if dr == nil || 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 != nil {
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 != nil {
67+
t.Fatal("Expected not to find DataRefExtension, but did")
68+
}
69+
}

0 commit comments

Comments
 (0)