Skip to content

Commit

Permalink
AVRO-3817 Make Aliases a Property of Field
Browse files Browse the repository at this point in the history
  • Loading branch information
kojiromike committed Jul 29, 2023
1 parent 383e194 commit 63dac52
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lang/py/avro/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"doc",
"order",
"type",
"aliases",
)

VALID_FIELD_SORT_ORDERS = (
Expand Down Expand Up @@ -429,7 +430,19 @@ def __init__(self, precision, scale=0, max_precision=0):
class Field(CanonicalPropertiesMixin, EqualByJsonMixin):
_reserved_properties: Sequence[str] = FIELD_RESERVED_PROPS

def __init__(self, type_, name, has_default, default=None, order=None, names=None, doc=None, other_props=None, validate_names: bool = True):
def __init__(
self,
type_,
name,
has_default,
default=None,
order=None,
names=None,
doc=None,
other_props=None,
validate_names: bool = True,
aliases: Optional[Collection[str]] = None,
):
if not name:
raise avro.errors.SchemaParseException("Fields must have a non-empty name.")
if not isinstance(name, str):
Expand All @@ -451,6 +464,7 @@ def __init__(self, type_, name, has_default, default=None, order=None, names=Non
self.type = type_schema
self.name = name
self.validate_names = validate_names
self.aliases = aliases
# TODO(hammer): check to ensure default is valid
if has_default:
self.set_prop("default", default)
Expand Down Expand Up @@ -1012,8 +1026,23 @@ def make_field_objects(field_data: Sequence[Mapping[str, object]], names: avro.n
default = field.get("default")
order = field.get("order")
doc = field.get("doc")
aliases = field.get("aliases") or ()
if not hasattr(aliases, "__contains__"):
raise avro.errors.SchemaParseException(f"Aliases for field {field} must be a collection of strings.")
aliases = cast(Collection[str], aliases)
other_props = get_other_props(field, FIELD_RESERVED_PROPS)
new_field = Field(type, name, has_default, default, order, names, doc, other_props, validate_names=validate_names)
new_field = Field(
type,
name,
has_default,
default,
order,
names,
doc,
other_props,
validate_names=validate_names,
aliases=aliases,
)
# make sure field name has not been used yet
if new_field.name in field_names:
fail_msg = f"Field name {new_field.name} already in use."
Expand Down

0 comments on commit 63dac52

Please sign in to comment.