Spaces:
Sleeping
Sleeping
Create schema_utils.py
Browse files- schema_utils.py +23 -0
schema_utils.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# schema_utils.py
|
| 2 |
+
from typing import Dict, Set
|
| 3 |
+
from schema_map import schema
|
| 4 |
+
|
| 5 |
+
def fieldset_for(collection: str) -> Set[str]:
|
| 6 |
+
col = schema.get(collection, {})
|
| 7 |
+
fields = {k for k in col.keys() if not k.startswith("$")}
|
| 8 |
+
# expand nested alias target names too
|
| 9 |
+
aliases = col.get("$aliases", {}) or {}
|
| 10 |
+
fields |= set(aliases.values())
|
| 11 |
+
return fields
|
| 12 |
+
|
| 13 |
+
def alias_map_for(collection: str) -> Dict[str, str]:
|
| 14 |
+
col = schema.get(collection, {})
|
| 15 |
+
return col.get("$aliases", {}) or {}
|
| 16 |
+
|
| 17 |
+
def resolve_field(collection: str, field: str) -> str:
|
| 18 |
+
"""Return the canonical field name (apply alias if present)."""
|
| 19 |
+
aliases = alias_map_for(collection)
|
| 20 |
+
return aliases.get(field, field)
|
| 21 |
+
|
| 22 |
+
def has_field(collection: str, field: str) -> bool:
|
| 23 |
+
return resolve_field(collection, field) in fieldset_for(collection)
|