Skip to content

Commit

Permalink
fix compatibility with newer avro versions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 committed Sep 5, 2024
1 parent 58bd87e commit ea375b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
29 changes: 20 additions & 9 deletions avrogen/avrojson.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections

import avro
import six
from avro import io, schema

Expand All @@ -11,11 +12,17 @@
io_validate = io.validate

try:
from avro.io import AvroTypeException, SchemaResolutionException
from avro.errors import AvroException, AvroTypeException, SchemaResolutionException
except ImportError:
from avro.errors import AvroTypeException, SchemaResolutionException
from avro.io import AvroException, AvroTypeException, SchemaResolutionException

try:
import avro.constants

_PRIMITIVE_TYPES = set(avro.constants.PRIMITIVE_TYPES)
except AttributeError:
_PRIMITIVE_TYPES = set(schema.PRIMITIVE_TYPES)

_PRIMITIVE_TYPES = set(schema.PRIMITIVE_TYPES)

_json_converter = None
_json_converter_tuples = None
Expand Down Expand Up @@ -125,9 +132,11 @@ def validate(self, expected_schema, datum, skip_logical_types=False) -> bool:
return all(
self.validate(
f.type,
datum.get(f.name, f.default)
if f.has_default
else datum.get(f.name),
(
datum.get(f.name, f.default)
if f.has_default
else datum.get(f.name)
),
skip_logical_types,
)
for f in expected_schema.fields
Expand Down Expand Up @@ -209,7 +218,7 @@ def _generic_to_json(self, data_obj, writers_schema, was_within_array=False):
if lt.validate(writers_schema, data_obj):
data_obj = lt.convert(writers_schema, data_obj)
else:
raise schema.AvroException(
raise AvroException(
"Wrong object for %s logical type"
% writers_schema.props.get("logicalType")
)
Expand All @@ -229,7 +238,7 @@ def _generic_to_json(self, data_obj, writers_schema, was_within_array=False):
elif writers_schema.type in ["union", "error_union"]:
result = self._union_to_json(data_obj, writers_schema, was_within_array)
else:
raise schema.AvroException("Invalid schema type: %s" % writers_schema.type)
raise AvroException("Invalid schema type: %s" % writers_schema.type)

return result

Expand Down Expand Up @@ -430,7 +439,9 @@ def _union_from_json(self, json_obj, writers_schema, readers_schema):
for s in writers_schema.schemas:
if self.validate(s, json_obj, skip_logical_types=True):
return self._generic_from_json(json_obj, s, readers_schema)
raise schema.AvroException("Datum union type not in schema: %s", value_type)
raise AvroTypeException(
f"Datum {json_obj} cannot be parsed as union schema {writers_schema}"
)

def _make_type(self, tp, record):
if issubclass(tp, DictWrapper):
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
https://github.com/acryldata/avro_gen
"""

from setuptools import setup, find_packages
from codecs import open
from os import path

from setuptools import find_packages, setup

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
Expand All @@ -20,7 +21,7 @@

setup(
name="avro-gen3",
version="0.7.13",
version="0.7.15",
description="Avro record class and specific record reader generator",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit ea375b6

Please sign in to comment.