Skip to content
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

feat: implementation for JSON format. #245

Draft
wants to merge 2 commits into
base: v2
Choose a base branch
from

Conversation

PlugaruT
Copy link

Changes

One line description for the changelog

  • Tests pass
  • Appropriate changes to README are included in PR

@PlugaruT
Copy link
Author

@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.

Copy link
Member

@xSAVIKx xSAVIKx left a 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.

src/cloudevents/core/formats/base.py Outdated Show resolved Hide resolved
src/cloudevents/core/formats/json.py Outdated Show resolved Hide resolved
src/cloudevents/core/formats/json.py Outdated Show resolved Hide resolved
return super().default(obj)


class JSONFormat(Format):
Copy link
Member

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?

Copy link
Author

@PlugaruT PlugaruT Nov 19, 2024

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.

@PlugaruT
Copy link
Author

We should be also having support for binary and batch modes from the start IMO.

Can you be more explicit, please? Batch mode is clear, but what does "binary" mean here?

@PlugaruT PlugaruT requested a review from xSAVIKx November 19, 2024 08:46
Comment on lines 20 to 41
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]: ...
Copy link
Member

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.

src/cloudevents/core/formats/json.py Outdated Show resolved Hide resolved
src/cloudevents/core/formats/base.py Outdated Show resolved Hide resolved
Signed-off-by: Tudor Plugaru <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants