mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 15:24:52 +09:00
Refactor JSON serialiable class pattern
Change the JSON seriasiable data classes to use an explicit static fromJSON method and an optional toJSON method for turning them to and from JSON. This improves over the previous pattern of passing the JSON to the constructor as it frees up the constructor to be able to take whichever argument types suits the code instead of being beholden to the JSON serialised form.
This commit is contained in:
1 parent
a448d87e3a
commit
9a967a5779
20 files changed
+222
-97
No files matched your search
@@ -385,7 +385,7 @@ class ControlConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
async createModPackRequestHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
if (this._controller.modPacks.has(modPack.id)) {
|
||||
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`);
|
||||
}
|
||||
@@ -394,7 +394,7 @@ class ControlConnection extends BaseConnection {
|
||||
}
|
||||
|
||||
async updateModPackRequestHandler(message) {
|
||||
let modPack = new libData.ModPack(message.data.mod_pack);
|
||||
let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
|
||||
if (!this._controller.modPacks.has(modPack.id)) {
|
||||
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
|
||||
}
|
||||
|
||||
@@ -413,7 +413,7 @@ class Controller {
|
||||
}
|
||||
return new Map();
|
||||
}
|
||||
return new Map(json.map(e => [e.id, new libData.ModPack(e)]));
|
||||
return new Map(json.map(e => [e.id, libData.ModPack.fromJSON(e)]));
|
||||
}
|
||||
|
||||
static async saveModPacks(filePath, modPacks) {
|
||||
|
||||
@@ -26,11 +26,9 @@ class InstanceInfo {
|
||||
*/
|
||||
status;
|
||||
|
||||
// This is not actually json as a config can't be loaded synchronously but
|
||||
// that is likely to change in the future.
|
||||
constructor(json) {
|
||||
this.config = json["config"];
|
||||
this.status = json["status"];
|
||||
constructor({ config, status }) {
|
||||
this.config = config;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
||||
@@ -232,7 +232,7 @@ async function uploadExport(req, res) {
|
||||
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer"));
|
||||
}
|
||||
|
||||
modPack.exportManifest = new libData.ExportManifest({ assets });
|
||||
modPack.exportManifest = new libData.ExportManifest(assets);
|
||||
modPack.fillDefaultSettings(settingPrototypes, logger);
|
||||
res.app.locals.controller.modPackUpdated(modPack);
|
||||
|
||||
|
||||
Reference in new issue
Block a user