-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgenerate_class_from_json.py
43 lines (34 loc) · 1.04 KB
/
generate_class_from_json.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
"""
Convert a JSON schema from the WebHDFS docs into a class
https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#ContentSummary_JSON_Schema
"""
import json
import sys
from typing import Any
TYPE_MAPPING = {
"integer": "int",
"string": "str",
}
def to_py_type(v: dict[str, Any]) -> str:
if "type" in v:
t = TYPE_MAPPING.get(v["type"], v["type"])
assert isinstance(t, str)
return t
if "enum" in v:
return "str"
raise AssertionError(v)
def main() -> None:
js = json.loads(sys.stdin.read())
name = js["name"]
print("class {}(_BoilerplateClass):".format(js["name"]))
print(' """')
for k, v in js["properties"][name]["properties"].items():
description = v.get("description", "")
print(f" :param {k}: {description}")
print(f" :type {k}: {to_py_type(v)}")
print(' """')
print()
for k, v in js["properties"][name]["properties"].items():
print(f" {k}: {to_py_type(v)}")
if __name__ == "__main__":
main()