- 
                Notifications
    You must be signed in to change notification settings 
- Fork 235
Adding a simple dataref extension #1018
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
Conversation
7c1fbd1    to
    e84d5f0      
    Compare
  
    | const DataRefExtensionKey = "dataref" | ||
|  | ||
| type DataRefExtension struct { | ||
| DataRef string `json:"dataref"` | 
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?
| yeah, to have it more flexible  🤷♂️
Sent from Gmail Mobile… On Tue 20. Feb 2024 at 13:30, Doug Davis ***@***.***> wrote:
 ***@***.**** commented on this pull request.
 ------------------------------
 In v2/extensions/dataref_extension.go
 <#1018 (comment)>:
 > +/*
 + 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"`
 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?
 —
 Reply to this email directly, view it on GitHub
 <#1018 (review)>,
 or unsubscribe
 <https://github.com/notifications/unsubscribe-auth/AABGPTUZAHNWNQI7YPPTCZDYUSJMZAVCNFSM6AAAAABDQULMHWVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQOJQGMYDSMZTGU>
 .
 You are receiving this because you authored the thread.Message ID:
 ***@***.***>
 | 
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.
LGTM
| 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") | ||
| } | ||
| } | 
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.
Looks like Java unit tests :)
nit: can we use table-driven tests so those tests are easier to modify/add later? Example here:
| func TestRequestWithRetries_linear(t *testing.T) { | 
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.
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 comment
The 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 comment
The 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.
ping @matzew
| rebase needed | 
e84d5f0    to
    40188ae      
    Compare
  
    40188ae    to
    58f801f      
    Compare
  
    58f801f    to
    f0848db      
    Compare
  
    f0848db    to
    09c9b2c      
    Compare
  
    | Added ref to #198 so it'll close it once we merge | 
09c9b2c    to
    26ade34      
    Compare
  
    | func TestGetDataRefExtensionNotFound(t *testing.T) { | ||
| e := event.New() | ||
|  | ||
| _, ok := GetDataRefExtension(e) | 
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.
nit: missing positive case getting extension
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.
doi!
26ade34    to
    9c287f1      
    Compare
  
    | Added more tests. | 
        
          
                v2/extensions/dataref_extension.go
              
                Outdated
          
        
      | } | ||
| return &DataRefExtension{DataRef: dataRefStr}, true | ||
| } | ||
| return nil, false | 
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.
Sorry Doug for this nit, but let's keep the value semantics here because using pointer can lead to (ownership) surprises (without checking the rest of the code, I assume we mostly use value semantics for core CE fields).
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.
Typically (convention?), if an error is returned, the value should not be used (we can add a comment to make it even more explicit). Alternatively, If needed, users can check for the zero value with https://pkg.go.dev/reflect#Value.IsZero.
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.
this is actually one of the reasons I prefer to return nil - I find the entire Zero value stuff annoying when nil is much more clear .  If we return a Zero struct then it's too easy for someone to think the dataref is set but with "" - which is a valid URL. But I see at least one of the other extensions does it without a pointer so I'll just hold my nose and switch it back.
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.
IIRC, Go is moving to making it easier to understand zero values (recently they did it with time for example) because nil has so many edges, most importantly surprising side effects for SDK users due to ownership/pointer semantics.
nit: we should add comments to these new public methods.
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.
done
| t.Fatalf("Retrieved dataref(%v) doesn't match set value(%s)", | ||
| dr, test.dataref) | ||
| } | ||
| } else { | 
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.
nit: I think it's safe to remove the else and directly jump to the next if without changing test semantics?
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.
I think we need it because we only want to fail when the dataref is set on the "failed" case, not when the pass is expected to pass.
9c287f1    to
    435db4a      
    Compare
  
    | @embano1 updated | 
Signed-off-by: Matthias Wessendorf <[email protected]> Signed-off-by: Doug Davis <[email protected]>
435db4a    to
    535da92      
    Compare
  
    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.
LGTM
Inspired by the java sdk, adding a go implementation for
datarefextensionFixes #198