-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding a simple dataref extension #1018
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2024 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package extensions | ||
|
||
import ( | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
"net/url" | ||
) | ||
|
||
const DataRefExtensionKey = "dataref" | ||
|
||
type DataRefExtension struct { | ||
DataRef string `json:"dataref"` | ||
} | ||
|
||
func AddDataRefExtension(e *event.Event, dataRef string) error { | ||
if _, err := url.Parse(dataRef); err != nil { | ||
return err | ||
} | ||
e.SetExtension(DataRefExtensionKey, dataRef) | ||
return nil | ||
} | ||
|
||
func GetDataRefExtension(e event.Event) (DataRefExtension, bool) { | ||
if dataRefValue, ok := e.Extensions()[DataRefExtensionKey]; ok { | ||
dataRefStr, ok := dataRefValue.(string) | ||
if !ok { | ||
return DataRefExtension{}, false | ||
} | ||
return DataRefExtension{DataRef: dataRefStr}, true | ||
} | ||
return DataRefExtension{}, false | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||
/* | ||||
Copyright 2024 The CloudEvents Authors | ||||
SPDX-License-Identifier: Apache-2.0 | ||||
*/ | ||||
|
||||
package extensions | ||||
|
||||
import ( | ||||
"testing" | ||||
|
||||
"github.com/cloudevents/sdk-go/v2/event" | ||||
) | ||||
|
||||
func TestAddDataRefExtensionValidURL(t *testing.T) { | ||||
e := event.New() | ||||
expectedDataRef := "https://example.com/data" | ||||
|
||||
err := AddDataRefExtension(&e, expectedDataRef) | ||||
if err != nil { | ||||
t.Fatalf("Failed to add DataRefExtension with valid URL: %s", err) | ||||
} | ||||
} | ||||
|
||||
func TestAddDataRefExtensionInvalidURL(t *testing.T) { | ||||
e := event.New() | ||||
invalidDataRef := "://invalid-url" | ||||
|
||||
err := AddDataRefExtension(&e, invalidDataRef) | ||||
if err == nil { | ||||
t.Fatal("Expected error when adding DataRefExtension with invalid URL, but got none") | ||||
} | ||||
} | ||||
|
||||
func TestGetDataRefExtensionNotFound(t *testing.T) { | ||||
e := event.New() | ||||
|
||||
_, ok := GetDataRefExtension(e) | ||||
if ok { | ||||
t.Fatal("Expected not to find DataRefExtension, but did") | ||||
} | ||||
} | ||||
Comment on lines
+14
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like Java unit tests :) nit: can we use table-driven tests so those tests are easier to modify/add later? Example here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed you might be following the distributed tracing implementation which uses same style...whatever you prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @matzew I'd like to merge this PR - did you want to update something based on this comment? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it matters much but I'm curious why it's a struct instead of just a string? Are you leaving the option open that one day we may need to include extra metadata along with the URL?