import { ConfigEntry, ConfigEntryType } from "../types" import { trimEnd } from "../utils"; export default function (data: ConfigEntry[]): string { let result = `using BililiveRecorder.Core.Config; using BililiveRecorder.Core.Config.V3; using GraphQL.Types; using HierarchicalPropertyDefault; #nullable enable `; function write_query_graphType_property(r: ConfigEntry) { if (r.configType == "roomOnly") { result += `this.Field(x => x.${r.id});\n`; } else { result += `this.Field(x => x.Optional${r.id}, type: typeof(HierarchicalOptionalType<${trimEnd(r.type, '?')}>));\n`; } } function write_rest_dto_property(r: ConfigEntry) { if (r.configType == "roomOnly") { result += `public ${r.type} ${r.id} { get; set; }\n`; } else { result += `public Optional<${r.type}> Optional${r.id} { get; set; }\n`; } } function write_mutation_graphType_property(r: ConfigEntry) { if (r.configType == "roomOnly") { result += `this.Field(x => x.${r.id}, nullable: true);\n`; } else { result += `this.Field(x => x.Optional${r.id}, nullable: true, type: typeof(HierarchicalOptionalInputType<${trimEnd(r.type, '?')}>));\n`; } } function write_mutation_dataType_property(r: ConfigEntry) { if (r.configType == "roomOnly") { result += `public ${r.type}? ${r.id} { get; set; }\n`; } else { result += `public Optional<${r.type}>? Optional${r.id} { get; set; }\n`; } } function write_mutation_apply_method(r: ConfigEntry) { if (r.configType == "roomOnly") { result += `if (this.${r.id}.HasValue) config.${r.id} = this.${r.id}.Value;\n`; } else { result += `if (this.Optional${r.id}.HasValue) config.Optional${r.id} = this.Optional${r.id}.Value;\n`; } } // +++++++++++++++++ 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' { // ====== RoomConfigType ====== result += "internal class RoomConfigType : ObjectGraphType\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\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\n{\n" result += "public DefaultConfigType()\n{\n"; data.filter(r => r.configType != "roomOnly") .forEach(r => { result += `this.Field(x => x.${r.id});\n`; }); result += "}\n}\n\n"; } { // ====== SetRoomConfigType ====== result += "internal class SetRoomConfigType : InputObjectGraphType\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\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; }