diff --git a/codegen/generator.py b/codegen/generator.py index 4cdcc56..b071d7b 100644 --- a/codegen/generator.py +++ b/codegen/generator.py @@ -99,6 +99,7 @@ "in": "requestBody", "required": True, # TODO get required } +TYPES = {} def fetch_openapi_specs(spec_url: str) -> dict: @@ -405,6 +406,34 @@ def checksum(dictionary: Dict[str, Any]) -> str: return dhash.hexdigest() +def generate_type(schema: dict, name: str) -> dict: + if schema["type"] == "dict": + schema["name"] = name + schema["template"] = "type_class" + schema["properties"] = generate_type_resolve_object(schema["properties"]) + print(schema) + exit(12) + else: + schema["name"] = get_param_name(name) + schema["template"] = "type_param" + if "description" not in schema or schema["description"].strip() == "": + schema["description"] = name + + # print(name) + # if schema["type"] == "dict": + # print(schema) + # exit(11) + return schema + +def generate_type_resolve_object(properties: dict) -> dict: + for name, prop in properties.items(): + if "type" in prop and prop["type"] == "dict": + properties[name]["name"] = name + #schema["properties"] = generate_type_resolve_object(schema["properties"]) + else: + properties[name]["name"] = get_param_name(name) + return properties + def _sanitize_filename(n: str) -> str: return n.replace(" ", "_").lower() @@ -413,6 +442,14 @@ def _sanitize_filename(n: str) -> str: # MAIN # # ------------------------------------------------------- # if __name__ == "__main__": + # Generate types skeleton + logging.info("creating types skeleton.") + out = Template(filename="codegen/templates/types.tpl", output_encoding="utf-8").render() + file_name = "%s/types.py" % WS_DIR + fh = open(file_name, "w+") + fh.write(out.decode("utf-8")) + fh.close() + for scope in SPECS.keys(): # fetch spec spec = fetch_openapi_specs(SPECS[scope]["spec_url"]) @@ -437,6 +474,25 @@ def _sanitize_filename(n: str) -> str: logging.info("resolving references ..") references = resolve_references(spec) + # generate types + for k, schema in spec["components"]["schemas"].items(): + id = f"#/components/schema/{k}" + if id not in TYPES: + if "type" in schema: + schema["id"] = id + TYPES[id] = generate_type(schema, k) + # print(TYPES) + + # write types + logging.info("writing types ..") + file_name = "%s/types.py" % WS_DIR + fh = open(file_name, "a+") + for t in TYPES.values(): + out = Template(filename="codegen/templates/%s.tpl" % t["template"], output_encoding="utf-8").render(**t) + fh.write(out.decode("utf-8")) + fh.close() + exit(11) + # generate namespaces logging.info("generating %d namespaces .." % len(namespaces)) it = 1 diff --git a/codegen/templates/type_class.tpl b/codegen/templates/type_class.tpl new file mode 100644 index 0000000..5aae118 --- /dev/null +++ b/codegen/templates/type_class.tpl @@ -0,0 +1,3 @@ +# ${description} +def ${name}(dict): + pass diff --git a/codegen/templates/type_param.tpl b/codegen/templates/type_param.tpl new file mode 100644 index 0000000..b94d5fa --- /dev/null +++ b/codegen/templates/type_param.tpl @@ -0,0 +1,3 @@ +# ${description} +# +${name}: ${type} diff --git a/codegen/templates/types.tpl b/codegen/templates/types.tpl new file mode 100644 index 0000000..53cd4ee --- /dev/null +++ b/codegen/templates/types.tpl @@ -0,0 +1,19 @@ +# +# Licensed to Xatabase, Inc under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Xatabase, Inc licenses this file to you under the +# Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You +# may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +