null with logical types
MODIFY
fastavro/writer.py
fastavro/writer.py
@@ -22,6 +22,7 @@ except ImportError:
from fastavro.schema import extract_named_schemas_into_repo,\
extract_record_type
+import datetime
try:
import simplejson as json
except ImportError:
@@ -184,15 +185,16 @@ def validate(datum, schema):
return isinstance(datum, bytes)
if record_type == 'int':
- return (
- isinstance(datum, (int, long,)) and
- INT_MIN_VALUE <= datum <= INT_MAX_VALUE
+ return (isinstance(datum, datetime.date) or
+ (isinstance(datum, (int, long,)) and
+ INT_MIN_VALUE <= datum <= INT_MAX_VALUE)
)
if record_type == 'long':
return (
- isinstance(datum, (int, long,)) and
- LONG_MIN_VALUE <= datum <= LONG_MAX_VALUE
+ isinstance(datum, datetime.datetime) or
+ (isinstance(datum, (int, long,)) and
+ LONG_MIN_VALUE <= datum <= LONG_MAX_VALUE)
)
if record_type in ['float', 'double']:

MODIFY
tests/test_logical_types.py
tests/test_logical_types.py
@@ -50,8 +50,30 @@ def test_logical_types():
data2 = deserialize(schema, binary)
assert (data1['date'] == data2['date'])
assert (data1['timestamp-micros'] == data2['timestamp-micros'])
- assert (int(data1['timestamp-millis'].microsecond / 1000) == int(data2[
- 'timestamp-millis'].microsecond))
+ assert (int(data1['timestamp-millis'].microsecond / 1000) ==
+ int(data2['timestamp-millis'].microsecond))
-test_logical_types()
+schema_null = {
+ "fields": [
+ {
+ "name": "date",
+ "type": ["null", {'type': 'int', 'logicalType': 'date'}]
+ },
+ ],
+ "namespace": "namespace",
+ "name": "name",
+ "type": "record"
+}
+
+
+def test_null():
+ data1 = {
+ # 'date': None,
+ }
+ binary = serialize(schema_null, data1)
+ data2 = deserialize(schema_null, binary)
+ assert (data2['date'] is None)
+
+
+test_null()

null with logical types