import json
from Cheetah.Template import Template
import glob

with open("schema.json") as schemaFile:
    schema = json.load(schemaFile)



for table in schema["tables"]:
    for column in table["columns"]:
        if column["sqltype"] == 'serial':
            column["apitype"] = 'integer'
            column["jstype"] = 'number'
        elif column["sqltype"] == 'integer':
            column["apitype"] = 'integer'
            column["jstype"] = 'number'
        elif column["sqltype"] == 'date':
            column["apitype"] = 'string'
            column["jstype"] = 'string'
        elif column["sqltype"] == 'timestamp':
            column["apitype"] = 'string'
            column["jstype"] = 'string'
        elif column["sqltype"].startswith('varchar'):
            column["apitype"] = 'string'
            column["jstype"] = 'string'
        elif column["sqltype"].startswith('numeric'):
            column["apitype"] = 'number'
            column["jstype"] = 'number'




for f in glob.glob("*.tmpl"):
    print(f"process {f}")
    tmpl = Template(file=f, searchList=[schema])
    with open(f[:-5], 'w') as outFile:
        outFile.write(str(tmpl))