-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a simple dataref extension, similar to the java sdk
Signed-off-by: Matthias Wessendorf <[email protected]>
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |