Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Request & Response types generator #142

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"in": "requestBody",
"required": True, # TODO get required
}
TYPES = {}


def fetch_openapi_specs(spec_url: str) -> dict:
Expand Down Expand Up @@ -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()

Expand All @@ -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"])
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions codegen/templates/type_class.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ${description}
def ${name}(dict):
pass
3 changes: 3 additions & 0 deletions codegen/templates/type_param.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ${description}
#
${name}: ${type}
19 changes: 19 additions & 0 deletions codegen/templates/types.tpl
Original file line number Diff line number Diff line change
@@ -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.
#