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 1 commit 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.

class Format(Protocol):
def read(self, data: Union[str, bytes]) -> BaseCloudEvent: ...

def write(self, event: BaseCloudEvent) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all formats may be represented as string. it's safer to have bytes here. Same applies to read method data attribute.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, my bad here

src/cloudevents/core/formats/json.py Show resolved Hide resolved
:return: The CloudEvent as a JSON formatted byte array.
"""
event_data = event.get_data()
event_dict: dict[str, Any] = {**event.get_attributes()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not?

Suggested change
event_dict: dict[str, Any] = {**event.get_attributes()}
event_dict: dict[str, Any] = dict(event.get_attributes())

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?

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 Show resolved Hide resolved
Comment on lines +21 to +24
class Format(Protocol):
def read(self, data: Union[str, bytes]) -> BaseCloudEvent: ...

def write(self, event: BaseCloudEvent) -> str: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in order to make this useful as a generic approach we should add generics :-)

Suggested change
class Format(Protocol):
def read(self, data: Union[str, bytes]) -> BaseCloudEvent: ...
def write(self, event: BaseCloudEvent) -> str: ...
T = TypeVar("T", bound=BaseCloudEvent)
class Format(Protocol[T]):
def read(self, data: bytes) -> T: ...
def write(self, event: T) -> bytes: ...

this way we should be able to get parameterized results as well

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, yes, thanks, I'll do that

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