Skip to content

Commit 87de9fa

Browse files
rnpridgeonRyan P
authored andcommitted
Flip arguments for Serializer/Deserializer
1 parent 21ae36a commit 87de9fa

File tree

8 files changed

+52
-57
lines changed

8 files changed

+52
-57
lines changed

confluent_kafka/deserializing_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def poll(self, timeout=-1):
130130
value = msg.value()
131131
if self._value_deserializer is not None:
132132
try:
133-
value = self._value_deserializer(ctx, value)
133+
value = self._value_deserializer(value, ctx)
134134
except SerializationError as se:
135135
raise ConsumeError(KafkaError._VALUE_DESERIALIZATION,
136136
reason=se.message,
@@ -140,7 +140,7 @@ def poll(self, timeout=-1):
140140
ctx.field = MessageField.KEY
141141
if self._key_deserializer is not None:
142142
try:
143-
key = self._key_deserializer(ctx, key)
143+
key = self._key_deserializer(key, ctx)
144144
except SerializationError as se:
145145
raise ConsumeError(KafkaError._KEY_DESERIALIZATION,
146146
reason=se.message,

confluent_kafka/schema_registry/avro.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class AvroSerializer(Serializer):
124124
125125
schema_str (str): Avro Schema declaration.
126126
127-
to_dict (callable, optional): Callable(SerializationContext, object) -> dict.
127+
to_dict (callable, optional): Callable(object, SerializationContext) -> dict.
128128
Converts object to a dict.
129129
130130
conf (dict): AvroSerializer configuration.
@@ -172,7 +172,7 @@ def __init__(self, schema_registry_client, schema_str,
172172

173173
if len(conf_copy) > 0:
174174
raise ValueError("Unrecognized properties: {}"
175-
.format(conf_copy.keys()))
175+
.format(", ".format(conf_copy.keys())))
176176

177177
# convert schema_str to Schema instance
178178
schema = _schema_loads(schema_str)
@@ -188,17 +188,17 @@ def __init__(self, schema_registry_client, schema_str,
188188
self._schema_name = schema_name
189189
self._parsed_schema = parsed_schema
190190

191-
def __call__(self, ctx, obj):
191+
def __call__(self, obj, ctx):
192192
"""
193193
Serializes an object to the Confluent Schema Registry's Avro binary
194194
format.
195195
196196
Args:
197+
obj (object): object instance to serializes.
198+
197199
ctx (SerializationContext): Metadata pertaining to the serialization
198200
operation.
199201
200-
obj (object): object instance to serialize.
201-
202202
Note:
203203
None objects are represented as Kafka Null.
204204
@@ -229,7 +229,7 @@ def __call__(self, ctx, obj):
229229
self._known_subjects.add(subject)
230230

231231
if self._to_dict is not None:
232-
value = self._to_dict(ctx, obj)
232+
value = self._to_dict(obj, ctx)
233233
else:
234234
value = obj
235235

@@ -261,7 +261,7 @@ class AvroDeserializer(Deserializer):
261261
262262
schema_str (str): Avro reader schema declaration.
263263
264-
from_dict (callable, optional): Callable(SerializationContext, dict) -> object.
264+
from_dict (callable, optional): Callable(dict, SerializationContext) -> object.
265265
Converts dict to an instance of some object.
266266
267267
.. _Schema declaration:
@@ -284,16 +284,16 @@ def __init__(self, schema_registry_client, schema_str, from_dict=None):
284284
" from_dict(SerializationContext, dict) -> object")
285285
self._from_dict = from_dict
286286

287-
def __call__(self, ctx, value):
287+
def __call__(self, value, ctx):
288288
"""
289289
Decodes a Confluent Schema Registry formatted Avro bytes to an object.
290290
291291
Arguments:
292+
value (bytes): bytes
293+
292294
ctx (SerializationContext): Metadata pertaining to the serialization
293295
operation.
294296
295-
value (bytes): bytes
296-
297297
Raises:
298298
SerializerError if an error occurs ready data.
299299
@@ -331,6 +331,6 @@ def __call__(self, ctx, value):
331331
self._reader_schema)
332332

333333
if self._from_dict is not None:
334-
return self._from_dict(ctx, obj_dict)
334+
return self._from_dict(obj_dict, ctx)
335335

336336
return obj_dict

confluent_kafka/serialization/__init__.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,16 @@ class Serializer(object):
137137
"""
138138
__slots__ = []
139139

140-
def __call__(self, ctx, obj):
140+
def __call__(self, obj, ctx):
141141
"""
142142
Converts obj to bytes.
143143
144144
Args:
145+
obj (object): object to be serialized
146+
145147
ctx (SerializationContext): Metadata pertaining to the serialization
146148
operation
147149
148-
obj (object): object to be serialized
149-
150150
Raises:
151151
SerializerError if an error occurs during serialization
152152
@@ -200,16 +200,16 @@ class Deserializer(object):
200200
"""
201201
__slots__ = []
202202

203-
def __call__(self, ctx, value):
203+
def __call__(self, value, ctx):
204204
"""
205205
Convert bytes to object
206206
207207
Args:
208+
value (bytes): bytes to be deserialized
209+
208210
ctx (SerializationContext): Metadata pertaining to the serialization
209211
operation
210212
211-
value (bytes): bytes to be deserialized
212-
213213
Raises:
214214
SerializerError if an error occurs during deserialization
215215
@@ -228,17 +228,16 @@ class DoubleSerializer(Serializer):
228228
https://docs.confluent.io/current/clients/javadocs/org/apache/kafka/common/serialization/DoubleSerializer.html
229229
230230
""" # noqa: E501
231-
232-
def __call__(self, ctx, obj):
231+
def __call__(self, obj, ctx):
233232
"""
234233
Serializes float as IEEE 764 binary64 bytes.
235234
236235
Args:
236+
obj (object): object to be serialized
237+
237238
ctx (SerializationContext): Metadata pertaining to the serialization
238239
operation
239240
240-
obj (object): object to be serialized
241-
242241
Note:
243242
None objects are represented as Kafka Null.
244243
@@ -266,17 +265,16 @@ class DoubleDeserializer(Deserializer):
266265
https://docs.confluent.io/current/clients/javadocs/org/apache/kafka/common/serialization/DoubleDeserializer.html
267266
268267
""" # noqa: E501
269-
270-
def __call__(self, ctx, value):
268+
def __call__(self, value, ctx):
271269
"""
272270
Deserializes float from IEEE 764 binary64 bytes.
273271
274272
Args:
273+
value (bytes): bytes to be deserialized
274+
275275
ctx (SerializationContext): Metadata pertaining to the serialization
276276
operation
277277
278-
value (bytes): bytes to be deserialized
279-
280278
Raises:
281279
SerializerError if an error occurs during deserialization.
282280
@@ -301,17 +299,16 @@ class IntegerSerializer(Serializer):
301299
https://docs.confluent.io/current/clients/javadocs/org/apache/kafka/common/serialization/IntegerSerializer.html
302300
303301
""" # noqa: E501
304-
305-
def __call__(self, ctx, obj):
302+
def __call__(self, obj, ctx):
306303
"""
307304
Serializes int as int32 bytes.
308305
309306
Args:
307+
obj (object): object to be serialized
308+
310309
ctx (SerializationContext): Metadata pertaining to the serialization
311310
operation
312311
313-
obj (object): object to be serialized
314-
315312
Note:
316313
None objects are represented as Kafka Null.
317314
@@ -339,17 +336,15 @@ class IntegerDeserializer(Deserializer):
339336
https://docs.confluent.io/current/clients/javadocs/org/apache/kafka/common/serialization/IntegerDeserializer.html
340337
341338
""" # noqa: E501
342-
343-
def __call__(self, ctx, value):
339+
def __call__(self, value, ctx):
344340
"""
345341
Deserializes int from int32 bytes.
346342
347343
Args:
348-
ctx (SerializationContext): Metadata pertaining to the serialization
349-
operation
350-
351344
value (bytes): bytes to be deserialized
352345
346+
ctx (SerializationContext): Metadata pertaining to the serialization
347+
operation
353348
354349
Raises:
355350
SerializerError if an error occurs during deserialization.
@@ -387,7 +382,7 @@ class StringSerializer(Serializer):
387382
def __init__(self, codec='utf_8'):
388383
self.codec = codec
389384

390-
def __call__(self, ctx, obj):
385+
def __call__(self, obj, ctx):
391386
"""
392387
Serializes a str(py2:unicode) to bytes.
393388
@@ -396,11 +391,11 @@ def __call__(self, ctx, obj):
396391
Python 3 all str objects are already unicode objects.
397392
398393
Args:
394+
obj (object): object to be serialized
395+
399396
ctx (SerializationContext): Metadata pertaining to the serialization
400397
operation
401398
402-
obj (object): object to be serialized
403-
404399
Raises:
405400
SerializerError if an error occurs during serialization.
406401
@@ -435,7 +430,7 @@ class StringDeserializer(Deserializer):
435430
def __init__(self, codec='utf_8'):
436431
self.codec = codec
437432

438-
def __call__(self, ctx, value):
433+
def __call__(self, value, ctx):
439434
"""
440435
Serializes unicode to bytes per the configured codec. Defaults to ``utf_8``.
441436
@@ -446,11 +441,11 @@ def __call__(self, ctx, value):
446441
Python 3 all str objects are already unicode objects.
447442
448443
Args:
444+
value (bytes): bytes to be deserialized
445+
449446
ctx (SerializationContext): Metadata pertaining to the serialization
450447
operation
451448
452-
value (bytes): bytes to be deserialized
453-
454449
Raises:
455450
SerializerError if an error occurs during deserialization.
456451

confluent_kafka/serializing_producer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ def produce(self, topic, key=None, value=None, partition=-1,
157157
"""
158158
ctx = SerializationContext(topic, MessageField.KEY)
159159
if self._key_serializer is not None:
160-
key = self._key_serializer(ctx, key)
160+
key = self._key_serializer(key, ctx)
161161

162162
ctx.field = MessageField.VALUE
163163
if self._value_serializer is not None:
164-
value = self._value_serializer(ctx, value)
164+
value = self._value_serializer(value, ctx)
165165

166166
super(SerializingProducer, self).produce(topic, value, key,
167167
headers=headers,

examples/avro_consumer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ def __init__(self, name=None, favorite_number=None, favorite_color=None):
4545
self.favorite_color = favorite_color
4646

4747

48-
def dict_to_user(ctx, obj):
48+
def dict_to_user(obj, ctx):
4949
"""
5050
Converts object literal(dict) to a User instance.
5151
5252
Args:
53+
obj (dict): Object literal(dict)
54+
5355
ctx (SerializationContext): Metadata pertaining to the serialization
5456
operation.
5557
56-
obj (dict): Object literal(dict)
57-
5858
"""
5959
if obj is None:
6060
return None

examples/avro_producer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ def __init__(self, name, address, favorite_number, favorite_color):
5252
self._address = address
5353

5454

55-
def user_to_dict(ctx, user):
55+
def user_to_dict(user, ctx):
5656
"""
5757
Returns a dict representation of a User instance for serialization.
5858
5959
Args:
60+
user (User): User instance.
61+
6062
ctx (SerializationContext): Metadata pertaining to the serialization
6163
operation.
6264
63-
user (User): User instance.
64-
6565
Returns:
6666
dict: Dict populated with user attributes to be serialized.
6767

tests/integration/schema_registry/test_avro_serializers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ def test_delivery_report_serialization(kafka_cluster, load_avsc, avsc, data, rec
123123
producer = kafka_cluster.producer(value_serializer=value_serializer)
124124

125125
def assert_cb(err, msg):
126-
actual = value_deserializer(SerializationContext(topic,
127-
MessageField.VALUE),
128-
msg.value())
126+
actual = value_deserializer(msg.value(),
127+
SerializationContext(topic, MessageField.VALUE))
129128

130129
if record_type == "record":
131130
assert [v == actual[k] for k, v in data.items()]
@@ -165,13 +164,13 @@ def test_avro_record_serialization_custom(kafka_cluster):
165164

166165
user = User('Bowie', 47, 'purple')
167166
value_serializer = AvroSerializer(sr, User.schema_str,
168-
lambda ctx, user:
167+
lambda user, ctx:
169168
dict(name=user.name,
170169
favorite_number=user.favorite_number,
171170
favorite_color=user.favorite_color))
172171

173172
value_deserializer = AvroDeserializer(sr, User.schema_str,
174-
lambda ctx, user_dict:
173+
lambda user_dict, ctx:
175174
User(**user_dict))
176175

177176
producer = kafka_cluster.producer(value_serializer=value_serializer)

tests/schema_registry/test_avro_serializer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ def test_avro_serializer_config_auto_register_schemas_false(mock_schema_registry
6363
test_serializer = AvroSerializer(test_client, "string",
6464
conf={'auto.register.schemas': False})
6565

66-
test_serializer(SerializationContext("test-auto-register",
67-
MessageField.KEY), "test")
66+
test_serializer("test",
67+
SerializationContext("test-auto-register",
68+
MessageField.KEY))
6869

6970
register_count = test_client.counter['POST'].get('/subjects/{}/versions'
7071
.format(subject), 0)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy