-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add support for schema_uri validation #87
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #87 +/- ##
==========================================
+ Coverage 82.29% 82.94% +0.64%
==========================================
Files 101 101
Lines 8028 8435 +407
==========================================
+ Hits 6607 6996 +389
- Misses 1196 1209 +13
- Partials 225 230 +5 ☔ View full report in Codecov by Sentry. |
d87ac46
to
cd46b71
Compare
Custom schemas can be loaded into the SDK. If a consumed event includes a custom schema, it can be loaded from the SDK local DB and the event can be validated accordingly. Added an example about parsing an event with a custom schema. Signed-off-by: Andrea Frittoli <[email protected]>
c01108b
to
10aa909
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.
Overall looks good, I think I had one comment that is necessary for me to approve this which was the silently failing one.
event.SetSource("my/first/cdevent/program") | ||
|
||
// Set the required subject fields | ||
event.SetSubjectContent(quotaRule123) |
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.
We really should look into optional parameters at some point. Makes using things a little easier.
event, err := cdeventsv04.NewCustomTypeEvent(cdevents.CustomTypeEventOptions.WithSchemaURI("my-schema"))
This adds some flexibility on where we can add validation as well, as in we could choose to do it upon creating the struct with a cdevents.WithQuickValidate()
or something as an option
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 was debating between explicit setters/getters and providing some kind of context object where users could stash fields to be set:
event.WithSubjectId("foo").WithSource("bar")
But that leads to as many WithX
methods as SetX
methods to be defined, and I didn't really see an advantage. For the subject content, I introduced SetSubjectContent
which allows setting the whole content from a Struct directly rather than field by field.
I'd like to better understand what you are proposing here, maybe something for the next WG.
ctx := cloudevents.ContextWithTarget(context.Background(), *source) | ||
ctx = cloudevents.WithEncodingBinary(ctx) | ||
|
||
c, err = cloudevents.NewClientHTTP() |
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.
Why don't we wrap the cloudevent library to hide all this from the user? This example would then just be something like
client, err := cdevents.NewHTTPClient()
if err != nil {
panic(err)
}
result, err := client.Send(event)
This becomes much simpler I feel like
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 on purpose - the CloudEvents API is rich, as it supports multiple transport options each with its own configuration capabilities. I don't think we should replicate that on CDEvents side, transport is not CDEvent's concern.
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.
Wrapping it does not mean a user cannot pass their own cloudevent client in. That can be done via optional params. This is standard in a lot of go sdks
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'm not convinced that:
client, err := cdevents.NewClientCloudEventsHTTP()
is going to provide a better user experience than:
client, err := cloudevents.NewClientHTTP()
The only difference is the import of cloudevents
, which is required anyway unless we wrap the IsUndelivered
method and others that may be required.
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 where my experience in SDKs come in. If you wrap it, you have full control on what it looks like to the user. You can add hooks, etc. With your current implementation you cannot without fully understanding two SDKs. Two! That's a bad experience
if err != nil { | ||
return err | ||
} | ||
// Check if there is a custom schema | ||
v4event, ok := event.(CDEventReaderV04) | ||
if ok { |
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.
So this just silently fails if it is not a CDEventReaderV04
? We already return errors, we should just return an error?
Unless this event reader can be something else, we should make it an interface in that case if it already isn'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.
Uhm, it doesn't fail. It checks if it's a CDEventReaderV04, and if it is it runs extra validation. If it isn't there's nothing else to be done in terms of validation.
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.
Should we allowing skipping? That seems really bad. Especially if they expect it to validate and it doesnt if it isnt that type
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.
Okay, after looking at this code some more. I wonder if this is the best approach. If we have multiple versions, would we keep appending if statements? I personally like the handler pattern for things like this. I'll add a github issue for that
Signed-off-by: Andrea Frittoli <[email protected]>
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 most of my comments is re-architecturing/refactoring, so I think with that said it isn't in scope for this PR. I'll go ahead and approve and follow up with the appropriate GH issues
Custom schemas can be loaded into the SDK.
If a consumed event includes a custom schema, it can be loaded from the SDK local DB and the event can be validated accordingly.
Added an example about parsing an event with a custom schema.