-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.py
95 lines (64 loc) · 2.35 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Convert Json schema to glue."""
from __future__ import annotations
from typing import Any, Union
import jsonref
from pydantic_glue.errors import GlueMapWithoutTypesError, ObjectWithoutPropertiesError, UnknownTypeError
def dispatch(v: dict[str, Any]) -> str: # noqa: PLR0911
"""Dispatch json schema element as glue type."""
if glue_type := v.get("glue_type"):
return str(glue_type)
if "anyOf" in v:
return _handle_union(v)
t = v["type"]
if t == "object":
return _handle_object(v)
if t == "array":
return _handle_array(v)
if t == "string":
if v.get("format") == "date-time":
return "timestamp"
if v.get("format") == "date":
return "date"
return "string"
if t == "boolean":
return "boolean"
if t == "integer":
return "int"
if t == "number":
return "float"
raise UnknownTypeError(t)
def _handle_map(o: dict[str, Any]) -> str:
t = o["additionalProperties"]
res = dispatch(t)
return f"map<string,{res}>"
def _handle_union(o: dict[str, Any]) -> str:
types = [i for i in o["anyOf"] if i["type"] != "null"]
if len(types) > 1:
res = [dispatch(v) for v in types]
return f"union<{','.join(res)}>"
return dispatch(types[0])
def _map_dispatch(o: dict[str, Any]) -> list[tuple[str, str]]:
return [(k, dispatch(v)) for k, v in o["properties"].items()]
def _handle_object(o: dict[str, Any]) -> str:
if "additionalProperties" in o:
if o["additionalProperties"] is True:
raise GlueMapWithoutTypesError
if o["additionalProperties"]:
if "properties" in o:
msg = "Merging types of properties and additionalProperties"
raise NotImplementedError(msg)
return _handle_map(o)
if "properties" not in o:
raise ObjectWithoutPropertiesError
res = [f"{k}:{v}" for (k, v) in _map_dispatch(o)]
return f"struct<{','.join(res)}>"
def _handle_array(o: dict[str, Any]) -> str:
t = dispatch(o["items"])
return f"array<{t}>"
def _handle_root(o: dict[str, Any]) -> list[tuple[str, str]]:
return _map_dispatch(o)
def convert(schema: str) -> Union[list[Any], list[tuple[str, str]]]:
"""Convert json schema to glue."""
if not schema:
return []
return _handle_root(jsonref.loads(schema))