BililiveRecorder/config_gen/generators/codeWeb.ts

162 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-08-13 21:03:21 +08:00
import { ConfigEntry, ConfigEntryType } from "../types"
2021-05-30 19:16:20 +08:00
import { trimEnd } from "../utils";
2021-08-13 21:03:21 +08:00
export default function (data: ConfigEntry[]): string {
2022-04-03 19:50:51 +08:00
let result = `using BililiveRecorder.Core.Config;
using BililiveRecorder.Core.Config.V3;
2021-05-30 19:16:20 +08:00
using GraphQL.Types;
using HierarchicalPropertyDefault;
#nullable enable
`;
function write_query_graphType_property(r: ConfigEntry) {
if (r.configType == "roomOnly") {
2022-06-10 16:34:25 +08:00
result += `this.Field(x => x.${r.id});\n`;
2021-05-30 19:16:20 +08:00
} else {
2022-06-10 16:34:25 +08:00
result += `this.Field(x => x.Optional${r.id}, type: typeof(HierarchicalOptionalType<${trimEnd(r.type, '?')}>));\n`;
2021-05-30 19:16:20 +08:00
}
}
function write_rest_dto_property(r: ConfigEntry) {
if (r.configType == "roomOnly") {
2022-06-10 16:34:25 +08:00
result += `public ${r.type} ${r.id} { get; set; }\n`;
} else {
2022-06-10 16:34:25 +08:00
result += `public Optional<${r.type}> Optional${r.id} { get; set; }\n`;
}
}
2021-05-30 19:16:20 +08:00
function write_mutation_graphType_property(r: ConfigEntry) {
if (r.configType == "roomOnly") {
2022-06-10 16:34:25 +08:00
result += `this.Field(x => x.${r.id}, nullable: true);\n`;
2021-05-30 19:16:20 +08:00
} else {
2022-06-10 16:34:25 +08:00
result += `this.Field(x => x.Optional${r.id}, nullable: true, type: typeof(HierarchicalOptionalInputType<${trimEnd(r.type, '?')}>));\n`;
2021-05-30 19:16:20 +08:00
}
}
function write_mutation_dataType_property(r: ConfigEntry) {
if (r.configType == "roomOnly") {
2022-06-10 16:34:25 +08:00
result += `public ${r.type}? ${r.id} { get; set; }\n`;
2021-05-30 19:16:20 +08:00
} else {
2022-06-10 16:34:25 +08:00
result += `public Optional<${r.type}>? Optional${r.id} { get; set; }\n`;
2021-05-30 19:16:20 +08:00
}
}
function write_mutation_apply_method(r: ConfigEntry) {
if (r.configType == "roomOnly") {
2022-06-10 16:34:25 +08:00
result += `if (this.${r.id}.HasValue) config.${r.id} = this.${r.id}.Value;\n`;
2021-05-30 19:16:20 +08:00
} else {
2022-06-10 16:34:25 +08:00
result += `if (this.Optional${r.id}.HasValue) config.Optional${r.id} = this.Optional${r.id}.Value;\n`;
2021-05-30 19:16:20 +08:00
}
}
// +++++++++++++++++ Shared +++++++++++++++++
result += 'namespace BililiveRecorder.Web.Models\n{\n'
{ // ====== SetRoomConfig ======
result += "public class SetRoomConfig\n{\n"
data.filter(x => x.configType != "globalOnly" && !x.webReadonly)
.forEach(r => write_mutation_dataType_property(r));
result += "\npublic void ApplyTo(RoomConfig config)\n{\n";
data.filter(x => x.configType != "globalOnly" && !x.webReadonly)
.forEach(r => write_mutation_apply_method(r));
result += "}\n}\n\n";
}
{ // ====== SetGlobalConfig ======
result += "public class SetGlobalConfig\n{\n"
data.filter(r => r.configType != "roomOnly" && !r.webReadonly)
.forEach(r => write_mutation_dataType_property(r));
result += "\npublic void ApplyTo(GlobalConfig config)\n{\n";
data.filter(r => r.configType != "roomOnly" && !r.webReadonly)
.forEach(r => write_mutation_apply_method(r));
result += "}\n}\n\n";
}
// +++++++++++++++++ REST +++++++++++++++++
result += '}\n\nnamespace BililiveRecorder.Web.Models.Rest\n{\n'
{ // ====== RoomConfigDto ======
result += "public class RoomConfigDto\n{\n"
data.filter(x => x.configType != "globalOnly" && !x.webReadonly)
.forEach(r => write_rest_dto_property(r));
result += "}\n\n";
}
{ // ====== GlobalConfigDto ======
result += "public class GlobalConfigDto\n{\n"
data.filter(r => r.configType != "roomOnly" && !r.webReadonly)
.forEach(r => write_rest_dto_property(r));
result += "}\n\n";
}
// +++++++++++++++++ Graphql +++++++++++++++++
result += '}\n\nnamespace BililiveRecorder.Web.Models.Graphql\n{\n'
2021-05-30 19:16:20 +08:00
{ // ====== RoomConfigType ======
result += "internal class RoomConfigType : ObjectGraphType<RoomConfig>\n{\n";
result += "public RoomConfigType()\n{\n"
data.filter(r => r.configType != "globalOnly").forEach(r => write_query_graphType_property(r));
result += "}\n}\n\n";
}
{ // ====== GlobalConfigType ======
result += "internal class GlobalConfigType : ObjectGraphType<GlobalConfig>\n{\n"
result += "public GlobalConfigType()\n{\n";
data.filter(r => r.configType != "roomOnly")
.forEach(r => write_query_graphType_property(r));
result += "}\n}\n\n";
}
{ // ====== DefaultConfigType ======
result += "internal class DefaultConfigType : ObjectGraphType<DefaultConfig>\n{\n"
result += "public DefaultConfigType()\n{\n";
data.filter(r => r.configType != "roomOnly")
.forEach(r => {
2022-06-10 16:34:25 +08:00
result += `this.Field(x => x.${r.id});\n`;
2021-05-30 19:16:20 +08:00
});
result += "}\n}\n\n";
}
{ // ====== SetRoomConfigType ======
result += "internal class SetRoomConfigType : InputObjectGraphType<SetRoomConfig>\n{\n"
result += "public SetRoomConfigType()\n{\n";
data.filter(x => x.configType != "globalOnly" && !x.webReadonly)
.forEach(r => write_mutation_graphType_property(r));
result += "}\n}\n\n";
}
{ // ====== SetGlobalConfigType ======
result += "internal class SetGlobalConfigType : InputObjectGraphType<SetGlobalConfig>\n{\n"
result += "public SetGlobalConfigType()\n{\n";
data.filter(r => r.configType != "roomOnly" && !r.webReadonly)
.forEach(r => write_mutation_graphType_property(r));
result += "}\n}\n\n";
}
result += `}\n`;
return result;
2021-08-13 21:03:21 +08:00
}