passthrough logical types for backwords compatibility
MODIFY
fastavro/writer.py
fastavro/writer.py
@@ -30,6 +30,9 @@ from os import urandom, SEEK_SET
from struct import pack
from zlib import compress
+import datetime
+import decimal
+
NoneType = type(None)
@@ -45,21 +48,32 @@ def write_boolean(fo, datum, schema=None):
def prepare_timestamp_millis(data, schema):
+ if isinstance(data, datetime.datetime):
t = int(time.mktime(data.timetuple())) * 1000 + int(
data.microsecond / 1000)
return t
+ else:
+ return data
def prepare_timestamp_micros(data, schema):
+ if isinstance(data, datetime.datetime):
t = int(time.mktime(data.timetuple())) * 1000000 + data.microsecond
return t
+ else:
+ return data
def prepare_date(data, schema):
+ if isinstance(data, datetime.date):
return data.toordinal()
+ else:
+ return data
def prepare_bytes_decimal(data, schema):
+ if not isinstance(data, decimal.Decimal):
+ return data
scale = schema['scale']
# based on https://github.com/apache/avro/pull/82/

MODIFY
tests/test_logical_types.py
tests/test_logical_types.py
@@ -5,8 +5,6 @@ from io import BytesIO
from nose.tools import raises
import fastavro
-from fastavro.reader import read_bytes_decimal
-
schema = {
"fields": [
@@ -59,6 +57,18 @@ def test_logical_types():
int(data2['timestamp-millis'].microsecond))
+def test_not_logical_ints():
+ data1 = {
+ 'date': 1,
+ 'timestamp-millis': 2,
+ 'timestamp-micros': 3,
+
+ }
+ binary = serialize(schema, data1)
+ data2 = deserialize(schema, binary)
+ assert (data2['date'] == datetime.date(1, 1, 1))
+
+
schema_null = {
"fields": [
{
@@ -113,6 +123,13 @@ def test_zero():
assert (data1 == data2)
+def test_bytes_decimal():
+ data1 = Decimal("123.456")
+ binary = serialize(schema_top, data1)
+ data2 = deserialize(schema_top, binary)
+ assert (data1 == data2)
+
+
schema_leftmost = {
"name": "n",
"namespace": "namespace",
@@ -124,9 +141,9 @@ schema_leftmost = {
def test_leftmost():
- binary = b'\xd5F\x80'
- res = read_bytes_decimal(binary, schema_leftmost)
- assert (Decimal("-2.80") == res)
+ binary = serialize(schema_leftmost, b'\xd5F\x80')
+ data2 = deserialize(schema_leftmost, binary)
+ assert (Decimal("-2.80") == data2)
@raises(AssertionError)

passthrough logical types for backwords compatibility