-
Notifications
You must be signed in to change notification settings - Fork 56
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
Pydantic v2 native implementation #219
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b0cab04
Create stub pydantic v2 implementation and parametrize tests for both…
febus982 b640eba
Add default values to optional fields
febus982 3e361df
Adapt pydantic v1 serializer/deserializer logic
febus982 692cb57
Extract CloudEvent fields non functional data in separate module
febus982 983cfd9
Fix lint
febus982 7b1e4a7
Add missing Copyright
febus982 ae4b362
Add missing docstring
febus982 7272113
Remove test leftover
febus982 ea2c85d
Remove dependency on HTTP CloudEvent implementation
febus982 85faf67
Remove failing test for unsupported scenario
febus982 f32ee91
Use SDK json serialization logic
febus982 a790f2f
No need to filter base64_data
febus982 b2e84cd
Use SDK json deserialization logic
febus982 f4265a1
Fix imports
febus982 bb7c70e
Move docs after field declarations
febus982 cd03614
Add test for model_validate_json method
febus982 892cd8b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b38efed
Use fully qualified imports
febus982 e73e3ab
Ignore typing error
febus982 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# Copyright 2018-Present The CloudEvents Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from cloudevents.sdk.event import attribute | ||
|
||
FIELD_DESCRIPTIONS = { | ||
"data": { | ||
"title": "Event Data", | ||
"description": ( | ||
"CloudEvents MAY include domain-specific information about the occurrence." | ||
" When present, this information will be encapsulated within data.It is" | ||
" encoded into a media format which is specified by the datacontenttype" | ||
" attribute (e.g. application/json), and adheres to the dataschema format" | ||
" when those respective attributes are present." | ||
), | ||
}, | ||
"source": { | ||
"title": "Event Source", | ||
"description": ( | ||
"Identifies the context in which an event happened. Often this will include" | ||
" information such as the type of the event source, the organization" | ||
" publishing the event or the process that produced the event. The exact" | ||
" syntax and semantics behind the data encoded in the URI is defined by the" | ||
" event producer.\n" | ||
"\n" | ||
"Producers MUST ensure that source + id is unique for" | ||
" each distinct event.\n" | ||
"\n" | ||
"An application MAY assign a unique source to each" | ||
" distinct producer, which makes it easy to produce unique IDs since no" | ||
" other producer will have the same source. The application MAY use UUIDs," | ||
" URNs, DNS authorities or an application-specific scheme to create unique" | ||
" source identifiers.\n" | ||
"\n" | ||
"A source MAY include more than one producer. In" | ||
" that case the producers MUST collaborate to ensure that source + id is" | ||
" unique for each distinct event." | ||
), | ||
"example": "https://github.com/cloudevents", | ||
}, | ||
"id": { | ||
"title": "Event ID", | ||
"description": ( | ||
"Identifies the event. Producers MUST ensure that source + id is unique for" | ||
" each distinct event. If a duplicate event is re-sent (e.g. due to a" | ||
" network error) it MAY have the same id. Consumers MAY assume that Events" | ||
" with identical source and id are duplicates. MUST be unique within the" | ||
" scope of the producer" | ||
), | ||
"example": "A234-1234-1234", | ||
}, | ||
"type": { | ||
"title": "Event Type", | ||
"description": ( | ||
"This attribute contains a value describing the type of event related to" | ||
" the originating occurrence. Often this attribute is used for routing," | ||
" observability, policy enforcement, etc. The format of this is producer" | ||
" defined and might include information such as the version of the type" | ||
), | ||
"example": "com.github.pull_request.opened", | ||
}, | ||
"specversion": { | ||
"title": "Specification Version", | ||
"description": ( | ||
"The version of the CloudEvents specification which the event uses. This" | ||
" enables the interpretation of the context.\n" | ||
"\n" | ||
"Currently, this attribute will only have the 'major'" | ||
" and 'minor' version numbers included in it. This allows for 'patch'" | ||
" changes to the specification to be made without changing this property's" | ||
" value in the serialization." | ||
), | ||
"example": attribute.DEFAULT_SPECVERSION, | ||
}, | ||
"time": { | ||
"title": "Occurrence Time", | ||
"description": ( | ||
" Timestamp of when the occurrence happened. If the time of the occurrence" | ||
" cannot be determined then this attribute MAY be set to some other time" | ||
" (such as the current time) by the CloudEvents producer, however all" | ||
" producers for the same source MUST be consistent in this respect. In" | ||
" other words, either they all use the actual time of the occurrence or" | ||
" they all use the same algorithm to determine the value used." | ||
), | ||
"example": "2018-04-05T17:31:00Z", | ||
}, | ||
"subject": { | ||
"title": "Event Subject", | ||
"description": ( | ||
"This describes the subject of the event in the context of the event" | ||
" producer (identified by source). In publish-subscribe scenarios, a" | ||
" subscriber will typically subscribe to events emitted by a source, but" | ||
" the source identifier alone might not be sufficient as a qualifier for" | ||
" any specific event if the source context has internal" | ||
" sub-structure.\n" | ||
"\n" | ||
"Identifying the subject of the event in context" | ||
" metadata (opposed to only in the data payload) is particularly helpful in" | ||
" generic subscription filtering scenarios where middleware is unable to" | ||
" interpret the data content. In the above example, the subscriber might" | ||
" only be interested in blobs with names ending with '.jpg' or '.jpeg' and" | ||
" the subject attribute allows for constructing a simple and efficient" | ||
" string-suffix filter for that subset of events." | ||
), | ||
"example": "123", | ||
}, | ||
"datacontenttype": { | ||
"title": "Event Data Content Type", | ||
"description": ( | ||
"Content type of data value. This attribute enables data to carry any type" | ||
" of content, whereby format and encoding might differ from that of the" | ||
" chosen event format." | ||
), | ||
"example": "text/xml", | ||
}, | ||
"dataschema": { | ||
"title": "Event Data Schema", | ||
"description": ( | ||
"Identifies the schema that data adheres to. " | ||
"Incompatible changes to the schema SHOULD be reflected by a different URI" | ||
), | ||
}, | ||
} | ||
|
||
""" | ||
The dictionary above contains title, description, example and other | ||
NON-FUNCTIONAL data for pydantic fields. It could be potentially. | ||
used across all the SDK. | ||
Functional field configurations (e.g. defaults) are still defined | ||
in the pydantic model classes. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2018-Present The CloudEvents Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from cloudevents.pydantic.v1.conversion import from_dict, from_http, from_json | ||
from cloudevents.pydantic.v1.event import CloudEvent | ||
|
||
__all__ = ["CloudEvent", "from_json", "from_dict", "from_http"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same regarding the exports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2018-Present The CloudEvents Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from cloudevents.pydantic.v2.conversion import from_dict, from_http, from_json | ||
from cloudevents.pydantic.v2.event import CloudEvent | ||
|
||
__all__ = ["CloudEvent", "from_json", "from_dict", "from_http"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd probably add "exports" here in the same style as we have it in
pydantic.__init__,py
.