-
Notifications
You must be signed in to change notification settings - Fork 69
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
A CloudEvent should be readonly but provide a way to augment itself #233
Comments
Sounds good. Going to assign to myself In the example, you are just talking about extensions, but i'm assuming this could go for any other value, like source or type or version, etc..? |
Yes. Though it's obviously not required, since using |
Something i was thinking about was trying to use a Proxy for this instead of Freezing the object and having another method that would be needed to "update" the CloudEvent. This way, whenever a property is updated, the Proxy can "trap" the set call and run the validate method. So doing something like this:
would trigger the validate method |
just realized not everyone might know what this Proxy object is, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy |
How about we make CloudEvents immutable like they aught to be and use |
I think #29 should be addressed first before this issue. |
After doing a bit of research on Proxies and a WIP PR #234 , it might be easier/more efficient to have a method an a cloudEvent that takes fields to update and merges then with the existing CloudEvents properties and then returns a new CloudEvent. sort of what @lance suggested with While i do like the proxy approach, the implementation was starting to get a bit convoluted. |
I would expect CloudEvents to be lightweight and immutable. If you want to clone or slightly modify a CloudEvent, I'd propose this radical idea: Create a const modifiedCE = new CloudEvent(oldCE, { id: 'my-new-id' }); Or I could imagine a new |
Yup. Doing something now. Probably sticking to the "cloneWith" method |
With recent API changes, a
CloudEvent
is nicely represented as a plain old JS object. For example, for most purposes, this is actually a valid CE.However, because we want to have spec compliance checks, and provide default values (e.g. a UUID or timestamp), we have a constructor and a class. The example above can be written as:
Here, code in the constructor is providing default values for
id
andspecversion
.However this introduces some tricky edge cases. For example, extensions. According to the spec, extensions must exist as siblings of the CE properties. And JS being as loosey goosey as it is, this means a user can do some things outside of the spec boundaries. For example
A solution to this is to
CloudEvent
constructor, callthis.validate()
CloudEvent
object read only just after validation withObject.freeze(this);
myEvent.cloneWith( { extension: 'value' });
(this could be a function onCloudEvent
or a class unto itself.Note that both #228 and #229 should be implemented and added to the validation step as a part of this.
Related: #29
The text was updated successfully, but these errors were encountered: