Repository navigation
Expand file tree
/
Copy pathgenerate_schemas.js
More file actions
executable file
·418 lines (385 loc) · 14.7 KB
/
Copy pathgenerate_schemas.js
File metadata and controls
executable file
·418 lines (385 loc) · 14.7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env node
// Node imports
import fs from "node:fs";
import path from "node:path";
import { parseArgs } from "node:util";
import process from "node:process";
import { fileURLToPath } from "node:url";
// Third party imports
import {
quicktype,
InputData,
JSONSchemaInput,
FetchingJSONSchemaStore,
splitIntoWords,
combineWords,
firstUpperWordStyle,
allUpperWordStyle,
legalizeCharacters,
isLetterOrDigit,
} from "quicktype-core";
import { glob } from "glob";
const projectName = path.basename(process.cwd()).toLowerCase().replaceAll("-", "_");
const args = parseArgs({
options: {
startDir: { type: "string" },
key: { type: "string" },
separator: { type: "string" },
prefix: { type: "string", default: projectName },
requireResponse: { type: "boolean", default: false },
},
});
console.log({ args });
const startDir = args.values.startDir;
const key = args.values.key;
const separator = args.values.separator;
const prefix = args.values.prefix;
const requireResponse = args.values.requireResponse;
const generatePython = startDir.split(path.sep).includes("src");
console.log("generatePython", generatePython);
const directoryPath = path.resolve(process.cwd(), startDir);
console.log("directoryPath", directoryPath);
const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`);
const outputJsFile = path.join(process.cwd(), `${projectName}_typed_schemas.js`);
const outputTypesFile = path.join(process.cwd(), `${projectName}_typed_schemas.d.ts`);
const errorSchemaFile = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"src",
"opengeodeweb_microservice",
"error.json",
);
// Route schema key holding the JSON schema of the success response body
const RESPONSE_KEY = "response";
// `"response": {"format": "binary"}`: the route streams a file instead of JSON
function isBinaryResponse(jsonData) {
return jsonData[RESPONSE_KEY]?.format === "binary";
}
// Type name quicktype gives to a top-level source: Python and TypeScript (default "pascal" acronym style) both upper-case acronyms, e.g. picked_ids -> PickedIDS
function quicktypeName(name) {
return combineWords(
splitIntoWords(name),
legalizeCharacters(isLetterOrDigit),
firstUpperWordStyle,
firstUpperWordStyle,
allUpperWordStyle,
allUpperWordStyle,
"",
isLetterOrDigit,
);
}
function assertGeneratedTypes(content, typeNames, pattern) {
for (const typeName of typeNames) {
if (!pattern(typeName).test(content)) {
throw new Error(`quicktype did not generate expected type ${typeName}`);
}
}
}
async function quicktypeJSONSchema(sources, lang, rendererOptions) {
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
for (const { name, schema } of sources) {
await schemaInput.addSource({ name, schema });
}
const inputData = new InputData();
inputData.addInput(schemaInput);
return await quicktype({ inputData, lang, rendererOptions });
}
function requestSchemaString(jsonData, fileContent) {
if (!(RESPONSE_KEY in jsonData)) {
return fileContent;
}
const requestSchema = JSON.parse(fileContent);
delete requestSchema[RESPONSE_KEY];
return JSON.stringify(requestSchema);
}
const PYTHON_OPTIONS = { "just-types": true, "python-version": "3.7" };
const PYTHON_CLASS_PATTERN = /@dataclass\nclass (\w+)(?:\s*\([^)]*\))?\s*:/g;
const PYTHON_CLASS_REPLACEMENT =
"@dataclass\nclass $1(DataClassJsonMixin):\n def __post_init__(self) -> None:\n print_dataclass(self)\n";
function pythonClassNames(content) {
return [...content.matchAll(PYTHON_CLASS_PATTERN)].map((match) => match[1]);
}
async function appendPythonResponse(filename, jsonData, requestContent) {
const paramsClass = quicktypeName(filename);
// Request classes stay exported as before; nested response classes are reached through their module,
// e.g. `schemas.allowed_objects.AllowedObject`, since several routes share the same nested names
const exportedNames = [...pythonClassNames(requestContent)];
let pythonContent = requestContent;
let responseClass = "BinaryResponse";
let schemasImport =
"from opengeodeweb_microservice.schemas import BinaryResponse, Route, load_schema";
if (!isBinaryResponse(jsonData)) {
responseClass = quicktypeName(`${filename}_response`);
schemasImport = "from opengeodeweb_microservice.schemas import Route, load_schema";
const { lines: responseTypes } = await quicktypeJSONSchema(
[{ name: `${filename}_response`, schema: JSON.stringify(jsonData[RESPONSE_KEY]) }],
"python",
PYTHON_OPTIONS,
);
const responseContent = responseTypes
.join("\n")
.replace(PYTHON_CLASS_PATTERN, PYTHON_CLASS_REPLACEMENT);
// Both quicktype runs name their nested classes independently, they must not shadow each other in the module
const requestClasses = new Set(exportedNames);
const duplicates = pythonClassNames(responseContent).filter((name) => requestClasses.has(name));
if (duplicates.length > 0) {
throw new Error(
`${filename}: request and response both define class(es) ${duplicates.join(", ")}, give them distinct "title"s`,
);
}
const isImport = (line) => /^(from|import) /.test(line);
const responseLines = responseContent.split("\n");
const newImports = responseLines.filter(
(line) => isImport(line) && !pythonContent.split("\n").includes(line),
);
const responseBody = responseLines
.filter((line) => !isImport(line))
.join("\n")
.trim();
pythonContent = [...newImports, pythonContent.trimEnd(), "", "", responseBody].join("\n");
exportedNames.push(responseClass);
}
assertGeneratedTypes(
pythonContent,
isBinaryResponse(jsonData) ? [paramsClass] : [paramsClass, responseClass],
(typeName) => new RegExp(`^class ${typeName}\\(`, "m"),
);
const routeName = `${filename}_route`;
exportedNames.push(routeName);
return (
"from pathlib import Path\n" +
schemasImport +
"\n" +
pythonContent.trimEnd() +
`\n\n\n${routeName} = Route(\n` +
` schema=load_schema(Path(__file__)),\n` +
` params=${paramsClass},\n` +
` response=${responseClass},\n` +
`)\n\n` +
`__all__ = [${exportedNames.map((name) => JSON.stringify(name)).join(", ")}]\n`
);
}
async function generatePythonFile(folderPath, filename, jsonData, fileContent) {
const { lines: requestTypes } = await quicktypeJSONSchema(
[{ name: filename, schema: requestSchemaString(jsonData, fileContent) }],
"python",
PYTHON_OPTIONS,
);
let pythonContent =
"from dataclasses_json import DataClassJsonMixin\n" +
"from opengeodeweb_microservice.schemas import print_dataclass\n" +
requestTypes.join("\n");
pythonContent = pythonContent.replace(PYTHON_CLASS_PATTERN, PYTHON_CLASS_REPLACEMENT);
if (RESPONSE_KEY in jsonData) {
pythonContent = await appendPythonResponse(filename, jsonData, pythonContent);
}
fs.writeFileSync(path.join(folderPath, filename + ".py"), pythonContent);
}
// Every request/response JSON schema, fed to a single quicktype run so TS type names are unique
const typescriptSources = [];
const typescriptTypeNames = new Map();
// Routes with the same file name in different folders (e.g. local/app/kill and local/extensions/kill) must not
// share a type name: quicktype would silently rename one of them (KillParams1) behind the Schemas tree's back
function registerTypescriptSource(sourceName, schema, filePath) {
const typeName = quicktypeName(sourceName);
if (typescriptTypeNames.has(typeName)) {
const error = new Error(
`${filePath}: TypeScript type ${typeName} already generated for ${typescriptTypeNames.get(typeName)}`,
);
error.fatal = true;
throw error;
}
typescriptTypeNames.set(typeName, filePath);
typescriptSources.push({ name: sourceName, schema });
return typeName;
}
function registerTypescriptTypes(folder_path, filename, filePath, jsonData, fileContent) {
// Root routes keep their plain name (AllowedFilesParams), nested ones get their folders (LocalAppKillParams)
const sourceName = [...folder_path.split("/"), filename].filter((part) => part).join("_");
const params = registerTypescriptSource(
`${sourceName}_params`,
requestSchemaString(jsonData, fileContent),
filePath,
);
let response = "unknown";
if (isBinaryResponse(jsonData)) {
response = "Blob";
} else if (RESPONSE_KEY in jsonData) {
response = registerTypescriptSource(
`${sourceName}_response`,
JSON.stringify(jsonData[RESPONSE_KEY]),
filePath,
);
}
return { params, response };
}
async function return_json_schema(directoryPath, folder_path, prefix) {
const folders = fs
.readdirSync(path.normalize(directoryPath), { withFileTypes: true })
.filter((folder) => folder.isDirectory() && folder.name != "__pycache__")
.map((folder) => ({
name: folder.name,
path: path.join(directoryPath, folder.name),
}));
var folders_schemas = {};
var folders_types = {};
for (const folder of folders) {
if (folder.name == "schemas") {
const jsonFiles = glob.sync(path.join(folder.path, "**/*.json"));
// Checked before touching any generated file, so a failure leaves the folder as it was
if (requireResponse) {
for (const filePath of jsonFiles) {
if (!(RESPONSE_KEY in JSON.parse(fs.readFileSync(filePath, "utf8")))) {
throw new Error(
`${filePath}: missing "${RESPONSE_KEY}" schema (required by --requireResponse)`,
);
}
}
}
if (generatePython) {
fs.readdirSync(folder.path)
.filter((file) => path.extname(file).toLowerCase() === ".py")
.forEach((file) => fs.unlinkSync(path.join(folder.path, file)));
}
var schemas = {};
let initContent = "";
for (const filePath of jsonFiles) {
const fileContent = fs.readFileSync(filePath, "utf8");
var jsonData = JSON.parse(fileContent);
var filename = filePath.replace(/^.*[\\/]/, "").replace(/\.[^/.]+$/, "");
try {
var route = jsonData[key];
var values = [prefix, folder_path, route];
values = values.map(function (value) {
return value.replace("/", "").replace(".", "");
});
values = values.map(function (value) {
return value.replaceAll("/", separator).replaceAll(".", separator);
});
jsonData["$id"] = values
.filter(function (val) {
return val;
})
.join(separator);
schemas[filename] = jsonData;
folders_types[filename] = registerTypescriptTypes(
folder_path,
filename,
filePath,
jsonData,
fileContent,
);
if (generatePython) {
initContent += "from ." + filename + " import *\n";
await generatePythonFile(folder.path, filename, jsonData, fileContent);
}
} catch (error) {
if (error.fatal) {
throw error;
}
console.error(`Erreur lors de la lecture du fichier ${filePath}:`, error);
}
}
if (generatePython) {
const initFile = path.join(folder.path, "__init__.py");
fs.writeFileSync(initFile, initContent);
}
folders_schemas = Object.keys(schemas).reduce((acc, key) => {
const currentSchema = schemas[key];
const modifiedSchema = {
$id: path.join(folder_path, currentSchema["$id"]),
...currentSchema,
};
acc[key] = modifiedSchema;
return acc;
}, folders_schemas);
} else {
var new_folder_path = folder_path + "/" + folder.name;
var test = await return_json_schema(folder.path, new_folder_path, prefix);
folders_schemas[folder.name] = test.schemas;
folders_types[folder.name] = test.types;
}
}
return { schemas: folders_schemas, types: folders_types };
}
function collectTypeNames(tree) {
return Object.values(tree).flatMap((value) =>
isTypesLeaf(value)
? [value.params, value.response].filter(
(typeName) => typeName !== "unknown" && typeName !== "Blob",
)
: collectTypeNames(value),
);
}
function isTypesLeaf(value) {
return typeof value.params === "string" && typeof value.response === "string";
}
// Mirror the JSON tree: each schema keeps its exact JSON type, intersected with its phantom request/response types
function typesTreeToTypescript(tree, jsonPath, indent) {
const pad = " ".repeat(indent);
const lines = Object.entries(tree).map(([name, value]) => {
const valuePath = `${jsonPath}[${JSON.stringify(name)}]`;
const type = isTypesLeaf(value)
? `${valuePath} & TypedSchema<${value.params}, ${value.response}>`
: typesTreeToTypescript(value, valuePath, indent + 1);
return `${pad} readonly ${name}: ${type};`;
});
return `{\n${lines.join("\n")}\n${pad}}`;
}
async function generateTypescript(typesTree) {
const sources = [
...typescriptSources,
{ name: "error_response", schema: fs.readFileSync(errorSchemaFile, "utf8") },
];
const { lines: tsTypes } = await quicktypeJSONSchema(sources, "typescript", {
"just-types": true,
"prefer-unions": true,
});
assertGeneratedTypes(
tsTypes.join("\n"),
collectTypeNames(typesTree),
(typeName) => new RegExp(`^export (interface|type) ${typeName}\\b`, "m"),
);
const content = [
"// Generated by opengeodeweb-microservice-generate, do not edit.",
`import type json from "./${path.basename(outputFile)}";`,
"",
tsTypes.join("\n"),
"// `__params` and `__response` only exist at type level, to infer request/response types from a schema.",
"// A type alias (not an interface) keeps the implicit index signature of the JSON type it is intersected with.",
"export type TypedSchema<Params, Response> = {",
" readonly __params?: Params;",
" readonly __response?: Response;",
"};",
"",
`export interface Schemas ${typesTreeToTypescript(typesTree, "(typeof json)", 0)}`,
"",
"declare const schemas: Schemas;",
"export default schemas;",
"",
].join("\n");
fs.writeFileSync(outputTypesFile, content);
fs.writeFileSync(
outputJsFile,
[
"// Generated by opengeodeweb-microservice-generate, do not edit.",
`import schemas from "./${path.basename(outputFile)}" with { type: "json" };`,
"",
"export default schemas;",
"",
].join("\n"),
);
}
async function main() {
const finalJson = {};
const finalTypes = {};
const { schemas, types } = await return_json_schema(directoryPath, "", prefix);
finalJson[prefix] = schemas;
finalTypes[prefix] = types;
console.log("FINAL", outputFile, finalJson);
fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2));
await generateTypescript(finalTypes);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});