Refactoring: split tagRenderingConfigJson into a version without and with questions

This commit is contained in:
Pieter Vander Vennet 2022-02-28 17:17:38 +01:00
parent c89bb32c61
commit 9f81628f64
12 changed files with 544 additions and 2348 deletions

View file

@ -6,7 +6,7 @@ interface JsonSchema {
description?: string,
type?: string,
properties?: any,
items?: JsonSchema | JsonSchema[],
items?: JsonSchema,
anyOf: JsonSchema[],
enum: JsonSchema[],
"$ref": string
@ -15,7 +15,6 @@ interface JsonSchema {
function WalkScheme<T>(
onEach: (schemePart: JsonSchema) => T,
scheme: JsonSchema,
registerSchemePath = false,
fullScheme: JsonSchema & { definitions?: any } = undefined,
path: string[] = [],
isHandlingReference = []
@ -24,6 +23,7 @@ function WalkScheme<T>(
if (scheme === undefined) {
return []
}
if (scheme["$ref"] !== undefined) {
const ref = scheme["$ref"]
const prefix = "#/definitions/"
@ -35,70 +35,48 @@ function WalkScheme<T>(
return;
}
const loadedScheme = fullScheme.definitions[definitionName]
return WalkScheme(onEach, loadedScheme, registerSchemePath, fullScheme, path, [...isHandlingReference, definitionName]);
return WalkScheme(onEach, loadedScheme, fullScheme, path, [...isHandlingReference, definitionName]);
}
fullScheme = fullScheme ?? scheme
var t = onEach(scheme)
if (t !== undefined) {
results.push({
path: [...path],
path,
t
})
}
function walk(v: JsonSchema, pathPart: string) {
function walk(v: JsonSchema) {
if (v === undefined) {
return
}
if (registerSchemePath) {
path.push("" + pathPart)
}
results.push(...WalkScheme(onEach, v, registerSchemePath, fullScheme, path, isHandlingReference))
if (registerSchemePath) {
path.pop()
}
results.push(...WalkScheme(onEach, v, fullScheme, path, isHandlingReference))
}
function walkEach(scheme: JsonSchema[], pathPart: string) {
function walkEach(scheme: JsonSchema[]) {
if (scheme === undefined) {
return
}
if (registerSchemePath) {
path.push("" + pathPart)
}
scheme.forEach((v, i) => walk(v, "" + i))
if (registerSchemePath) {
path.pop()
}
scheme.forEach(v => walk(v))
}
{
walkEach(scheme.enum, "enum")
walkEach(scheme.anyOf, "anyOf")
if (scheme.items !== undefined) {
if (scheme.items["forEach"] !== undefined) {
walkEach(<any>scheme.items, "items")
} else {
walk(<any>scheme.items, "items")
}
walkEach(scheme.enum)
walkEach(scheme.anyOf)
if (Array.isArray(scheme.items)) {
walkEach(<any>scheme.items)
} else {
walk(<any>scheme.items)
}
if (registerSchemePath) {
path.push("properties")
}
for (const key in scheme.properties) {
const prop = scheme.properties[key]
path.push(key)
results.push(...WalkScheme(onEach, prop, registerSchemePath, fullScheme, path, isHandlingReference))
path.pop()
}
if (registerSchemePath) {
path.pop()
results.push(...WalkScheme(onEach, prop, fullScheme, [...path, key], isHandlingReference))
}
}
@ -114,14 +92,16 @@ function extractMeta(typename: string, path: string) {
const typeHint = schemePart.description.split("\n")
.find(line => line.trim().toLocaleLowerCase().startsWith("type:"))
?.substr("type:".length)?.trim()
const type = schemePart.type ?? schemePart.anyOf;
const type = schemePart.items?.anyOf ?? schemePart.type ?? schemePart.anyOf;
return {typeHint, type}
}, themeSchema)
writeFileSync("./assets/" + path + ".json", JSON.stringify(withTypes.map(({
path,
t
}) => ({path, ...t})), null, " "))
const paths = withTypes.map(({
path,
t
}) => ({path, ...t}))
writeFileSync("./assets/" + path + ".json", JSON.stringify(paths, null, " "))
console.log("Written meta to ./assets/" + path)
}
@ -148,6 +128,7 @@ function main() {
extractMeta("LayoutConfigJson", "layoutconfigmeta")
extractMeta("TagRenderingConfigJson", "tagrenderingconfigmeta")
extractMeta("QuestionableTagRenderingConfigJson", "questionabletagrenderingconfigmeta")
}