BililiveRecorder/config_gen/generators/codeSchema.ts

104 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-08-13 21:03:21 +08:00
import { ConfigEntry, ConfigEntryType } from "../types"
2022-06-10 16:34:25 +08:00
function mapTypeToJsonSchema(id: string, type: string, defaultValue: any) {
2021-08-13 21:03:21 +08:00
switch (type) {
case "RecordMode":
return { type: "integer", default: 0, enum: [0, 1], "description": "0: Standard\n1: Raw" };
case "CuttingMode":
return { type: "integer", default: 0, enum: [0, 1, 2], "description": "0: 禁用\n1: 根据时间切割\n2: 根据文件大小切割" };
2022-06-10 16:34:25 +08:00
case "AllowedAddressFamily":
return { type: "integer", default: 0, enum: [-1, 0, 1, 2], "description": "-1: 由系统决定\n0: 任意 IPv4 或 IPv6\n1: 仅 IPv4\n2: IPv6" };
2021-08-13 21:03:21 +08:00
case "uint":
2022-06-10 16:34:25 +08:00
return { type: "integer", minimum: 0, maximum: 4294967295, default: defaultValue };
2021-08-13 21:03:21 +08:00
case "int":
2022-06-10 16:34:25 +08:00
return { type: "integer", minimum: -2147483648, maximum: 2147483647, default: defaultValue };
2021-08-13 21:03:21 +08:00
case "bool":
2022-06-10 16:34:25 +08:00
return { type: "boolean", default: defaultValue };
2021-08-13 21:03:21 +08:00
case "string":
case "string?":
2022-06-10 16:34:25 +08:00
if (id === 'Cookie') {
2021-08-13 21:03:21 +08:00
return { type: "string", pattern: "^(\S+=\S+;? ?)*$", maxLength: 4096, };
}
2022-06-10 16:34:25 +08:00
return { type: "string", default: defaultValue };
2021-08-13 21:03:21 +08:00
default:
return { type, default: defaultValue };
}
}
function buildProperty(target: { [i: string]: any }, config: ConfigEntry) {
2022-06-10 16:34:25 +08:00
const typeObj = mapTypeToJsonSchema(config.id, config.type, config.default);
2021-08-13 21:03:21 +08:00
2022-06-10 16:34:25 +08:00
if (config.default === 'default')
2021-08-13 21:03:21 +08:00
delete typeObj['default'];
2022-06-10 16:34:25 +08:00
target[config.id] = {
description: config.name
+ '\n默认: ' + config.default,
markdownDescription: config.name
+ ' \n默认: `' + config.default
+ ' `\n\n',
2021-08-13 21:03:21 +08:00
type: "object",
additionalProperties: false,
properties: {
HasValue: {
type: "boolean",
default: true
},
Value: typeObj
}
};
}
export default function (data: ConfigEntry[]): string {
const sharedConfig = {};
const globalConfig = {};
const roomConfig = {};
data.filter(x => x.configType == 'room').forEach(v => buildProperty(sharedConfig, v));
data.filter(x => x.configType == 'roomOnly').forEach(v => buildProperty(roomConfig, v));
data.filter(x => x.configType == 'globalOnly').forEach(v => buildProperty(globalConfig, v));
const schema = {
"$comment": "GENERATED CODE, DO NOT EDIT MANUALLY.",
"$schema": "http://json-schema.org/schema",
"definitions": {
"global-config": {
"description": "全局设置",
"additionalProperties": false,
"properties": { ...globalConfig, ...sharedConfig }
},
"room-config": {
"description": "房间独立设置",
"additionalProperties": false,
"properties": { ...roomConfig, ...sharedConfig }
}
},
"type": "object",
"additionalProperties": false,
"required": [
"$schema",
"version"
],
"properties": {
"$schema": {
"type": "string",
2022-06-10 16:34:25 +08:00
"default": "https://raw.githubusercontent.com/BililiveRecorder/BililiveRecorder/dev/configV3.schema.json"
2021-08-13 21:03:21 +08:00
},
"version": {
2022-06-10 16:34:25 +08:00
"const": 3
2021-08-13 21:03:21 +08:00
},
"global": {
"$ref": "#/definitions/global-config"
},
"rooms": {
"type": "array",
"items": {
"$ref": "#/definitions/room-config"
}
}
}
}
return JSON.stringify(schema, null, 2)
}