Skip to content

Commit 54f03fe

Browse files
committed
add Any, record & enum support
1 parent bad984e commit 54f03fe

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

schema_salad/avro/schema.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ def is_subtype(existing: PropType, new: PropType) -> bool:
672672
return True
673673
if isinstance(existing, list) and (new in existing):
674674
return True
675+
if existing == "Any":
676+
if new is None or new == [] or new == ["null"] or new == "null":
677+
return False
678+
if isinstance(new, list) and "null" in new:
679+
return False
680+
return True
675681
if (
676682
isinstance(existing, dict)
677683
and "type" in existing
@@ -681,6 +687,33 @@ def is_subtype(existing: PropType, new: PropType) -> bool:
681687
and new["type"] == "array"
682688
):
683689
return is_subtype(existing["items"], new["items"])
690+
if (
691+
isinstance(existing, dict)
692+
and "type" in existing
693+
and existing["type"] == "enum"
694+
and isinstance(new, dict)
695+
and "type" in new
696+
and new["type"] == "enum"
697+
):
698+
return is_subtype(existing["symbols"], new["symbols"])
699+
if (
700+
isinstance(existing, dict)
701+
and "type" in existing
702+
and existing["type"] == "record"
703+
and isinstance(new, dict)
704+
and "type" in new
705+
and new["type"] == "record"
706+
):
707+
for new_field in cast(List[Dict[str, Any]], new["fields"]):
708+
new_field_missing = True
709+
for existing_field in cast(List[Dict[str, Any]], existing["fields"]):
710+
if new_field["name"] == existing_field["name"]:
711+
if not is_subtype(existing_field["type"], new_field["type"]):
712+
return False
713+
new_field_missing = False
714+
if new_field_missing:
715+
return False
716+
return True
684717
if isinstance(existing, list) and isinstance(new, list):
685718
missing = False
686719
for _type in new:

schema_salad/tests/test_subtypes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@
1414
{"type": "array", "items": ["int", "float"]},
1515
True,
1616
),
17+
("Any", "int", True),
18+
("Any", ["int", "null"], False),
19+
("Any", ["int"], True),
20+
(
21+
{"type": "enum", "symbols": ["homo_sapiens", "mus_musculus"]},
22+
{"type": "enum", "symbols": ["homo_sapiens"]},
23+
True,
24+
),
25+
(
26+
{"type": "enum", "symbols": ["homo_sapiens", "mus_musculus"]},
27+
{"type": "enum", "symbols": ["homo_sapiens", "drosophila_melanogaster"]},
28+
False,
29+
),
30+
(
31+
{"type": "record", "fields": [{"name": "species", "type": "string"}]},
32+
{"type": "enum", "symbols": ["homo_sapiens"]},
33+
False,
34+
),
35+
(
36+
{
37+
"type": "record",
38+
"fields": [
39+
{"name": "species", "type": "string"},
40+
{"name": "id", "type": "int"},
41+
],
42+
},
43+
{"type": "record", "fields": [{"name": "species", "type": "string"}]},
44+
True,
45+
),
1746
]
1847

1948

0 commit comments

Comments
 (0)