-
Notifications
You must be signed in to change notification settings - Fork 61
feat: implementation for JSON format. #245
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
…tion included. Signed-off-by: Tudor Plugaru <[email protected]>
|
@xSAVIKx This is a draft implementation for JSON format specs. Can you please have a look and provide some feedback about the direction I choose? I have to admit that it's inspired by the Java implementation a bit. |
xSAVIKx
left a 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.
@PlugaruT PTAL at the comments noted below. We should be also having support for binary and batch modes from the start IMO.
| return super().default(obj) | ||
|
|
||
|
|
||
| class JSONFormat(Format): |
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.
how do you envision usage of this class by the end users?
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.
Technically, there should be no need for this class to be used often, unless someone knows what they are doing.
Since for each binding, we would have to implement both structured and binary modes, this format class would be useful when using structured mode, since the entire CloudEvent class is serialized into JSON/Avro and send as a single message. This format would be injected into that "structured mode CE writer" implementation. Something like
to_structured(event, format):
...
from_structured(data, format):
...I can't see this class being used for binary mode tho, correct me if I'm wrong please. But given that attributes are sent as message metadata, it can only be JSON format by default, no?
Anyway, I think it's worth having a way for having CE -> bytes and bytes -> CE for each supported format as per the specs and let engineers do manipulations as they see fit.
Can you be more explicit, please? Batch mode is clear, but what does "binary" mean here? |
src/cloudevents/core/base.py
Outdated
| class BaseCloudEvent(Protocol): | ||
| def get_id(self) -> str: ... | ||
|
|
||
| def get_source(self) -> str: ... | ||
|
|
||
| def get_type(self) -> str: ... | ||
|
|
||
| def get_specversion(self) -> str: ... | ||
|
|
||
| def get_datacontenttype(self) -> Optional[str]: ... | ||
|
|
||
| def get_dataschema(self) -> Optional[str]: ... | ||
|
|
||
| def get_subject(self) -> Optional[str]: ... | ||
|
|
||
| def get_time(self) -> Optional[datetime]: ... | ||
|
|
||
| def get_extension(self, extension_name: str) -> Any: ... | ||
|
|
||
| def get_data(self) -> Optional[Union[dict, str, bytes]]: ... | ||
|
|
||
| def get_attributes(self) -> dict[str, Any]: ... |
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.
It's probably a good idea to move the docs for these methods from the implementation to protocol.
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.
@PlugaruT, can you please address this? that's gonna be required for a library
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 missed this one. Fixed now
Signed-off-by: Tudor Plugaru <[email protected]>
Signed-off-by: Tudor Plugaru <[email protected]>
Signed-off-by: Tudor Plugaru <[email protected]>
Signed-off-by: Tudor Plugaru <[email protected]>
|
@xSAVIKx if you want to see the larger picture, there's this PlugaruT#1 that I developed on top of this PR. HTTP and Kafka bindings. Let me know your thoughts |
xSAVIKx
left a 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.
@PlugaruT it looks good. there are just 2 comments. Would you be able to address those?
src/cloudevents/core/formats/base.py
Outdated
| class Format(Protocol): | ||
| def read( | ||
| self, | ||
| event_factory: Callable[ | ||
| [dict, Optional[Union[dict, str, bytes]]], BaseCloudEvent | ||
| ], | ||
| data: Union[str, bytes], | ||
| ) -> BaseCloudEvent: ... | ||
|
|
||
| def write(self, event: BaseCloudEvent) -> bytes: ... |
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.
can you please move/add some docs here?
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
src/cloudevents/core/base.py
Outdated
| class BaseCloudEvent(Protocol): | ||
| def get_id(self) -> str: ... | ||
|
|
||
| def get_source(self) -> str: ... | ||
|
|
||
| def get_type(self) -> str: ... | ||
|
|
||
| def get_specversion(self) -> str: ... | ||
|
|
||
| def get_datacontenttype(self) -> Optional[str]: ... | ||
|
|
||
| def get_dataschema(self) -> Optional[str]: ... | ||
|
|
||
| def get_subject(self) -> Optional[str]: ... | ||
|
|
||
| def get_time(self) -> Optional[datetime]: ... | ||
|
|
||
| def get_extension(self, extension_name: str) -> Any: ... | ||
|
|
||
| def get_data(self) -> Optional[Union[dict, str, bytes]]: ... | ||
|
|
||
| def get_attributes(self) -> dict[str, Any]: ... |
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.
@PlugaruT, can you please address this? that's gonna be required for a library
Signed-off-by: Tudor Plugaru <[email protected]>
Signed-off-by: Tudor Plugaru <[email protected]>
|
@xSAVIKx if you approve, can you please also merge, doesn't seem I can do it. Thanks |
Changes
One line description for the changelog