Skip to content

Commit cf5616b

Browse files
Release/v1.7.0 (#201)
* chore: Fix typings errors and cleanup code a bit Signed-off-by: Yurii Serhiichuk <[email protected]> * chore: Use `AnyStr` shortcut instead of `Union[bytes, str]` Signed-off-by: Yurii Serhiichuk <[email protected]> * chore: Bump version. Signed-off-by: Yurii Serhiichuk <[email protected]> * Update the changelog Signed-off-by: Yurii Serhiichuk <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Signed-off-by: Yurii Serhiichuk <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent de61dd9 commit cf5616b

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.7.0] — 2022-11-17
10+
### Added
11+
- Added [Kafka](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/kafka-protocol-binding.md)
12+
support ([#197], thanks [David Martines](https://github.com/davidwmartines))
13+
914
## [1.6.2] — 2022-10-18
1015
### Added
1116
- Added `get_attributes` API to the `CloudEvent` API. The method returns a read-only
@@ -152,6 +157,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
152157
### Added
153158
- Initial release
154159

160+
[1.7.0]: https://github.com/cloudevents/sdk-python/compare/1.6.0...1.7.0
155161
[1.6.2]: https://github.com/cloudevents/sdk-python/compare/1.6.1...1.6.2
156162
[1.6.1]: https://github.com/cloudevents/sdk-python/compare/1.6.0...1.6.1
157163
[1.6.0]: https://github.com/cloudevents/sdk-python/compare/1.5.0...1.6.0
@@ -218,3 +224,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
218224
[#188]: https://github.com/cloudevents/sdk-python/pull/188
219225
[#191]: https://github.com/cloudevents/sdk-python/pull/191
220226
[#195]: https://github.com/cloudevents/sdk-python/pull/195
227+
[#197]: https://github.com/cloudevents/sdk-python/pull/197

cloudevents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
__version__ = "1.6.2"
15+
__version__ = "1.7.0"

cloudevents/kafka/conversion.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,18 @@ class KafkaMessage(typing.NamedTuple):
3838
The dictionary of message headers key/values.
3939
"""
4040

41-
key: typing.Optional[typing.Union[bytes, str]]
41+
key: typing.Optional[typing.AnyStr]
4242
"""
4343
The message key.
4444
"""
4545

46-
value: typing.Union[bytes, str]
46+
value: typing.AnyStr
4747
"""
4848
The message value.
4949
"""
5050

5151

52-
KeyMapper = typing.Callable[[AnyCloudEvent], typing.Union[bytes, str]]
52+
KeyMapper = typing.Callable[[AnyCloudEvent], typing.AnyStr]
5353
"""
5454
A callable function that creates a Kafka message key, given a CloudEvent instance.
5555
"""
@@ -174,7 +174,7 @@ def to_structured(
174174
f"Failed to map message key with error: {type(e).__name__}('{e}')"
175175
)
176176

177-
attrs = event.get_attributes().copy()
177+
attrs: dict[str, typing.Any] = dict(event.get_attributes())
178178

179179
try:
180180
data = data_marshaller(event.data)
@@ -208,7 +208,7 @@ def from_structured(
208208
message: KafkaMessage,
209209
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
210210
data_unmarshaller: typing.Optional[types.MarshallerType] = None,
211-
envelope_unmarshaller: typing.Optional[types.MarshallerType] = None,
211+
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
212212
) -> AnyCloudEvent:
213213
"""
214214
Returns a CloudEvent from a KafkaMessage in structured format.
@@ -232,20 +232,20 @@ def from_structured(
232232
"Failed to unmarshall message with error: " f"{type(e).__name__}('{e}')"
233233
)
234234

235-
attributes = {}
235+
attributes: dict[str, typing.Any] = {}
236236
if message.key is not None:
237237
attributes["partitionkey"] = message.key
238238

239+
data: typing.Optional[typing.Any] = None
239240
for name, value in structure.items():
240-
decoder = lambda x: x
241-
if name == "data":
242-
decoder = lambda v: data_unmarshaller(v)
243-
if name == "data_base64":
244-
decoder = lambda v: data_unmarshaller(base64.b64decode(v))
245-
name = "data"
246-
247241
try:
248-
decoded_value = decoder(value)
242+
if name == "data":
243+
decoded_value = data_unmarshaller(value)
244+
elif name == "data_base64":
245+
decoded_value = data_unmarshaller(base64.b64decode(value))
246+
name = "data"
247+
else:
248+
decoded_value = value
249249
except Exception as e:
250250
raise cloud_exceptions.DataUnmarshallerError(
251251
"Failed to unmarshall data with error: " f"{type(e).__name__}('{e}')"

cloudevents/tests/test_kafka_conversions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def custom_marshaller(self) -> types.MarshallerType:
7171
return simple_serialize
7272

7373
@pytest.fixture
74-
def custom_unmarshaller(self) -> types.MarshallerType:
74+
def custom_unmarshaller(self) -> types.UnmarshallerType:
7575
return simple_deserialize
7676

7777
def test_custom_marshaller_can_talk_to_itself(

0 commit comments

Comments
 (0)