-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pydantic v2 native implementation (#219)
* Create stub pydantic v2 implementation and parametrize tests for both implementations Signed-off-by: Federico Busetti <[email protected]> * Add default values to optional fields Signed-off-by: Federico Busetti <[email protected]> * Adapt pydantic v1 serializer/deserializer logic Signed-off-by: Federico Busetti <[email protected]> * Extract CloudEvent fields non functional data in separate module Signed-off-by: Federico Busetti <[email protected]> * Fix lint Signed-off-by: Federico Busetti <[email protected]> * Add missing Copyright Signed-off-by: Federico Busetti <[email protected]> * Add missing docstring Signed-off-by: Federico Busetti <[email protected]> * Remove test leftover Signed-off-by: Federico Busetti <[email protected]> * Remove dependency on HTTP CloudEvent implementation Signed-off-by: Federico Busetti <[email protected]> * Remove failing test for unsupported scenario Fix typo Signed-off-by: Federico Busetti <[email protected]> * Use SDK json serialization logic Signed-off-by: Federico Busetti <[email protected]> * No need to filter base64_data Signed-off-by: Federico Busetti <[email protected]> * Use SDK json deserialization logic Signed-off-by: Federico Busetti <[email protected]> * Fix imports Signed-off-by: Federico Busetti <[email protected]> * Move docs after field declarations Signed-off-by: Federico Busetti <[email protected]> * Add test for model_validate_json method Signed-off-by: Federico Busetti <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Use fully qualified imports Signed-off-by: Federico Busetti <[email protected]> * Ignore typing error Signed-off-by: Federico Busetti <[email protected]> --------- Signed-off-by: Federico Busetti <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e5f76ed
commit 5a1063e
Showing
12 changed files
with
790 additions
and
240 deletions.
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
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.