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:
Hornwitser committed 2023-02-25 00:02:12 +01:00
1 parent a448d87e3a
commit 9a967a5779
20 files changed
+222 -97

No files matched your search

+6 -6
View File
@@ -22,6 +22,10 @@ class ExportManifest {
*/
assets = {};
constructor(assets) {
if (assets) { this.assets = assets; }
}
static jsonSchema = {
type: "object",
additionalProperties: false,
@@ -34,12 +38,8 @@ class ExportManifest {
},
};
constructor(json = {}) {
if (json.assets) { this.assets = json.assets; }
}
toJSON() {
return { assets: this.assets };
static fromJSON(json) {
return new this(json.assets);
}
}
+18 -14
View File
@@ -155,22 +155,26 @@ class ModInfo {
static validate = libSchema.compile(this.jsonSchema);
constructor(json = {}) {
static fromJSON(json) {
const modInfo = new this();
// info.json fields
if (json.name) { this.name = json.name; }
if (json.version) { this.version = json.version; }
if (json.title) { this.title = json.title; }
if (json.author) { this.author = json.author; }
if (json.contact) { this.contact = json.contact; }
if (json.homepage) { this.homepage = json.homepage; }
if (json.description) { this.description = json.description; }
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
if (json.dependencies) { this.dependencies = json.dependencies; }
if (json.name) { modInfo.name = json.name; }
if (json.version) { modInfo.version = json.version; }
if (json.title) { modInfo.title = json.title; }
if (json.author) { modInfo.author = json.author; }
if (json.contact) { modInfo.contact = json.contact; }
if (json.homepage) { modInfo.homepage = json.homepage; }
if (json.description) { modInfo.description = json.description; }
if (json.factorio_version) { modInfo.factorioVersion = json.factorio_version; }
if (json.dependencies) { modInfo.dependencies = json.dependencies; }
// Additional data
if (json.size) { this.size = json.size; }
if (json.sha1) { this.sha1 = json.sha1; }
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
if (json.size) { modInfo.size = json.size; }
if (json.sha1) { modInfo.sha1 = json.sha1; }
if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
return modInfo;
}
toJSON() {
@@ -218,7 +222,7 @@ class ModInfo {
throw new Error(`Mod's version (${modInfo.version}) is invalid`);
}
return new this({
return this.fromJSON({
...modInfo,
size: (await fs.stat(modPath)).size,
+18 -14
View File
@@ -142,29 +142,33 @@ class ModPack {
static validate = libSchema.compile(this.jsonSchema);
constructor(json = {}) {
static fromJSON(json) {
const modPack = new this();
if (json.id) {
this.id = json.id;
modPack.id = json.id;
} else {
this.id = Math.random() * 2**31 | 0;
modPack.id = Math.random() * 2**31 | 0;
}
if (json.name) { this.name = json.name; }
if (json.description) { this.description = json.description; }
if (json.factorio_version) { this.factorioVersion = json.factorio_version; }
if (json.mods) { this.mods = new Map(json.mods.map(m => [m.name, m])); }
if (json.name) { modPack.name = json.name; }
if (json.description) { modPack.description = json.description; }
if (json.factorio_version) { modPack.factorioVersion = json.factorio_version; }
if (json.mods) { modPack.mods = new Map(json.mods.map(m => [m.name, m])); }
if (json.settings) {
this.settings = {
modPack.settings = {
"startup": new Map(Object.entries(json.settings["startup"])),
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])),
};
}
if (json.export_manifest) { this.exportManifest = new ExportManifest(json.export_manifest); }
if (json.is_deleted) { this.isDeleted = json.is_deleted; }
if (json.export_manifest) { modPack.exportManifest = ExportManifest.fromJSON(json.export_manifest); }
if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
if (!this.mods.has("base")) {
this.mods.set("base", { name: "base", enabled: true, version: this.factorioVersion });
if (!modPack.mods.has("base")) {
modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
}
return modPack;
}
toJSON() {
@@ -180,7 +184,7 @@ class ModPack {
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]),
},
};
if (this.exportManifest) { json.export_manifest = this.exportManifest.toJSON(); }
if (this.exportManifest) { json.export_manifest = this.exportManifest; }
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
return json;
}
@@ -217,7 +221,7 @@ class ModPack {
throw new Error("Malformed mod pack string: Schema validation failed");
}
return new this(json);
return this.fromJSON(json);
}
toModSettingsDat() {