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

+119
View File
@@ -0,0 +1,119 @@
# JSON Serialisable Classes
To serialise and deserialise data the Clusterio codebase uses a JSON serialisable data class pattern.
Instances of these classes can be serialised to JSON by passing them directly to `JSON.serialise` and deserialised by passing the serialised JSON representation to its static `fromJSON` method.
The data class also has a static `jsonSchema` property describing the serialised JSON representation for validation purposes.
See the following example code for a hypothetical `Data` class with a single property `member` defaulting to a value of `"foo"`.
```js
// Data.js
"use strictc";
/**
* A Data class example
* @alias module:Data
*/
class Data {
/** @type {string} */
member = "foo";
constructor(member) {
if (member !== undefined) { this.member = member; }
}
static jsonSchema = {
type: "object",
properties: {
"member": { type: "string" },
},
};
static fromJSON(json) {
return new this(json.member);
}
// toJSON is called by JSON.stringify when serialising this class
toJSON() {
let json = {};
// Omit property if it's set to the default value
if (this.member !== "foo") { json["member"] = this.member; }
return json;
}
}
module.exports = Data;
```
Serialising an instance of `Data` for JSON is as simple as doing `serialised = JSON.stringify(data)` as the `toJSON` method is automatically called by `JSON.stringify` to create a suitable object.
Deserialising is done by calling the static `fromJSON` method with the serialised JSON data, like `data = Data.fromJson(JSON.parse(serialised))`.
By structuring the classes like this they can be easily composed together.
For example a class holding a data property with an instance of the Data class from the previous example can be created as follows.
```js
// Composed.js
"use strict";
const Data = require("./Data");
/**
* Composed data class example
* @alias module:Data
*/
class Composed {
/** @type {module:Data|undefined} */
data;
constructor(data) {
if (data) { this.data = data; }
}
static jsonSchema = {
type: "object",
properties: {
"data": Data.jsonSchema,
},
};
static fromJSON(json) {
let data;
if (json["data"]) { data = Data.fromJSON(json["data"]); }
return new this(data);
}
// toJSON is optional and not needed for this particular class
}
module.exports = Composed;
```
It's easy to create a new instance of Composed by calling `new Composed(new Data("spam"))` and serialising to and from JSON is the same as for Data.
Meaning `serialised = JSON.stringify(composed)` and `composed = Composed.fromJSON(JSON.parse(serialised))` does as expected.
## Interface description
Data classes are ES6 style class declarations with the following methods and fields:
### toJSON(): any (optional)
Method returning an object or primitive suitable for passing to JSON.stringify in order to create a JSON representation of the class instance.
The returned object may contain objects that have their own toJSON methods which JSON.stringify will then call recursively.
This method is optional if calling `JSON.stringify` on an instance without it would produce the desired serialised form, however this is rarely the case as often you'll have to provide it to convert the values fram Map/Set properites, omit default values or do similar actions.
Note that the primary use case for serialising data classes is to send them over the network, this method should therefore try to reduce the size of the resulting JSON by for example omitting properties who's values are the default value.
### static fromJSON(json: any): this
Method constructing an instance of this class from one that was previously serialised by passing an instance of it to `JSON.stringify`.
While not a requirement it is preferrable if the input is not mutated and that no references to any any structures in the input ends up in the returned object.
In other words calling fromJSON twice with the same input should produce two independent objects.
Note that directly passing the output of `toJSON` to `fromJSON` is not valid, as the output of `toJSON` may require further expansion calling `toJSON` on objects returned by it.
It has to go through `JSON.stringify` or an equivalent recursive expansion of `toJSON`.
### static jsonSchema
A [JSON schema](http://json-schema.org/) describing the format of the JSON resulting from passing an instance of this class to `JSON.stringify`.
This should be lenient towards adding additional properties that would be ignored by `fromJSON` as it makes it easier to extend these classes in the future without breaking compatibility.
+2 -2
View File
@@ -385,7 +385,7 @@ class ControlConnection extends BaseConnection {
} }
async createModPackRequestHandler(message) { 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)) { if (this._controller.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`); throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} already exist`);
} }
@@ -394,7 +394,7 @@ class ControlConnection extends BaseConnection {
} }
async updateModPackRequestHandler(message) { 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)) { if (!this._controller.modPacks.has(modPack.id)) {
throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`); throw new libErrors.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
} }
+1 -1
View File
@@ -413,7 +413,7 @@ class Controller {
} }
return new Map(); 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) { static async saveModPacks(filePath, modPacks) {
+3 -5
View File
@@ -26,11 +26,9 @@ class InstanceInfo {
*/ */
status; status;
// This is not actually json as a config can't be loaded synchronously but constructor({ config, status }) {
// that is likely to change in the future. this.config = config;
constructor(json) { this.status = status;
this.config = json["config"];
this.status = json["status"];
} }
toJSON() { toJSON() {
+1 -1
View File
@@ -232,7 +232,7 @@ async function uploadExport(req, res) {
await libFileOps.safeOutputFile(path.join("static", `${name}.${hash}${ext}`), await file.async("nodebuffer")); 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); modPack.fillDefaultSettings(settingPrototypes, logger);
res.app.locals.controller.modPackUpdated(modPack); res.app.locals.controller.modPackUpdated(modPack);
+4 -4
View File
@@ -1002,7 +1002,7 @@ modPackCommands.add(new libCommand.Command({
}); });
}], }],
handler: async function(args, control) { handler: async function(args, control) {
const modPack = new libData.ModPack(); const modPack = libData.ModPack.fromJSON({});
modPack.name = args.name; modPack.name = args.name;
if (args.description) { modPack.description = args.description; } if (args.description) { modPack.description = args.description; }
if (args.factorioVersion) { if (args.factorioVersion) {
@@ -1038,7 +1038,7 @@ modPackCommands.add(new libCommand.Command({
const response = await libLink.messages.getModPack.send(control, { const response = await libLink.messages.getModPack.send(control, {
id: await libCommand.resolveModPack(control, args.modPack), id: await libCommand.resolveModPack(control, args.modPack),
}); });
const modPack = new libData.ModPack(response.mod_pack); const modPack = libData.ModPack.fromJSON(response.mod_pack);
print(modPack.toModPackString()); print(modPack.toModPackString());
}, },
})); }));
@@ -1065,7 +1065,7 @@ modPackCommands.add(new libCommand.Command({
let response = await libLink.messages.getModPack.send(control, { let response = await libLink.messages.getModPack.send(control, {
id: await libCommand.resolveModPack(control, args.modPack), id: await libCommand.resolveModPack(control, args.modPack),
}); });
let modPack = new libData.ModPack(response.mod_pack); let modPack = libData.ModPack.fromJSON(response.mod_pack);
if (args.name) { modPack.name = args.name; } if (args.name) { modPack.name = args.name; }
if (args.description) { modPack.description = args.description; } if (args.description) { modPack.description = args.description; }
@@ -1259,7 +1259,7 @@ modCommands.add(new libCommand.Command({
} }
if (result.body.mods && result.body.mods.length) { if (result.body.mods && result.body.mods.length) {
const mod = new libData.ModInfo(result.body.mods[0]); const mod = libData.ModInfo.fromJSON(result.body.mods[0]);
logger.info(`Successfully uploaded ${mod.filename}`); logger.info(`Successfully uploaded ${mod.filename}`);
} }
+1 -1
View File
@@ -604,7 +604,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
} else { } else {
response = await libLink.messages.getModPack.send(this, { id: modPackId }); response = await libLink.messages.getModPack.send(this, { id: modPackId });
} }
this.activeModPack = new libData.ModPack(response.mod_pack); this.activeModPack = libData.ModPack.fromJSON(response.mod_pack);
// TODO validate factorioVersion // TODO validate factorioVersion
+6 -6
View File
@@ -22,6 +22,10 @@ class ExportManifest {
*/ */
assets = {}; assets = {};
constructor(assets) {
if (assets) { this.assets = assets; }
}
static jsonSchema = { static jsonSchema = {
type: "object", type: "object",
additionalProperties: false, additionalProperties: false,
@@ -34,12 +38,8 @@ class ExportManifest {
}, },
}; };
constructor(json = {}) { static fromJSON(json) {
if (json.assets) { this.assets = json.assets; } return new this(json.assets);
}
toJSON() {
return { assets: this.assets };
} }
} }
+18 -14
View File
@@ -155,22 +155,26 @@ class ModInfo {
static validate = libSchema.compile(this.jsonSchema); static validate = libSchema.compile(this.jsonSchema);
constructor(json = {}) { static fromJSON(json) {
const modInfo = new this();
// info.json fields // info.json fields
if (json.name) { this.name = json.name; } if (json.name) { modInfo.name = json.name; }
if (json.version) { this.version = json.version; } if (json.version) { modInfo.version = json.version; }
if (json.title) { this.title = json.title; } if (json.title) { modInfo.title = json.title; }
if (json.author) { this.author = json.author; } if (json.author) { modInfo.author = json.author; }
if (json.contact) { this.contact = json.contact; } if (json.contact) { modInfo.contact = json.contact; }
if (json.homepage) { this.homepage = json.homepage; } if (json.homepage) { modInfo.homepage = json.homepage; }
if (json.description) { this.description = json.description; } if (json.description) { modInfo.description = json.description; }
if (json.factorio_version) { this.factorioVersion = json.factorio_version; } if (json.factorio_version) { modInfo.factorioVersion = json.factorio_version; }
if (json.dependencies) { this.dependencies = json.dependencies; } if (json.dependencies) { modInfo.dependencies = json.dependencies; }
// Additional data // Additional data
if (json.size) { this.size = json.size; } if (json.size) { modInfo.size = json.size; }
if (json.sha1) { this.sha1 = json.sha1; } if (json.sha1) { modInfo.sha1 = json.sha1; }
if (json.is_deleted) { this.isDeleted = json.is_deleted; } if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
return modInfo;
} }
toJSON() { toJSON() {
@@ -218,7 +222,7 @@ class ModInfo {
throw new Error(`Mod's version (${modInfo.version}) is invalid`); throw new Error(`Mod's version (${modInfo.version}) is invalid`);
} }
return new this({ return this.fromJSON({
...modInfo, ...modInfo,
size: (await fs.stat(modPath)).size, size: (await fs.stat(modPath)).size,
+18 -14
View File
@@ -142,29 +142,33 @@ class ModPack {
static validate = libSchema.compile(this.jsonSchema); static validate = libSchema.compile(this.jsonSchema);
constructor(json = {}) { static fromJSON(json) {
const modPack = new this();
if (json.id) { if (json.id) {
this.id = json.id; modPack.id = json.id;
} else { } else {
this.id = Math.random() * 2**31 | 0; modPack.id = Math.random() * 2**31 | 0;
} }
if (json.name) { this.name = json.name; } if (json.name) { modPack.name = json.name; }
if (json.description) { this.description = json.description; } if (json.description) { modPack.description = json.description; }
if (json.factorio_version) { this.factorioVersion = json.factorio_version; } if (json.factorio_version) { modPack.factorioVersion = json.factorio_version; }
if (json.mods) { this.mods = new Map(json.mods.map(m => [m.name, m])); } if (json.mods) { modPack.mods = new Map(json.mods.map(m => [m.name, m])); }
if (json.settings) { if (json.settings) {
this.settings = { modPack.settings = {
"startup": new Map(Object.entries(json.settings["startup"])), "startup": new Map(Object.entries(json.settings["startup"])),
"runtime-global": new Map(Object.entries(json.settings["runtime-global"])), "runtime-global": new Map(Object.entries(json.settings["runtime-global"])),
"runtime-per-user": new Map(Object.entries(json.settings["runtime-per-user"])), "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.export_manifest) { modPack.exportManifest = ExportManifest.fromJSON(json.export_manifest); }
if (json.is_deleted) { this.isDeleted = json.is_deleted; } if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
if (!this.mods.has("base")) { if (!modPack.mods.has("base")) {
this.mods.set("base", { name: "base", enabled: true, version: this.factorioVersion }); modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
} }
return modPack;
} }
toJSON() { toJSON() {
@@ -180,7 +184,7 @@ class ModPack {
"runtime-per-user": Object.fromEntries(this.settings["runtime-per-user"]), "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; } if (this.isDeleted) { json.is_deleted = this.isDeleted; }
return json; return json;
} }
@@ -217,7 +221,7 @@ class ModPack {
throw new Error("Malformed mod pack string: Schema validation failed"); throw new Error("Malformed mod pack string: Schema validation failed");
} }
return new this(json); return this.fromJSON(json);
} }
toModSettingsDat() { toModSettingsDat() {
@@ -57,7 +57,7 @@ function SearchModsTable(props) {
// In React < v18 this causes 3 renders and a partial state necessitating the use // In React < v18 this causes 3 renders and a partial state necessitating the use
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React. // of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
setModResults(response.results.map( setModResults(response.results.map(
({ name, versions }) => ({ name, versions: versions.map(mod => new libData.ModInfo(mod)) }) ({ name, versions }) => ({ name, versions: versions.map(mod => libData.ModInfo.fromJSON(mod)) })
)); ));
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0]))); setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
setModResultCount(response.result_count); setModResultCount(response.result_count);
@@ -630,7 +630,7 @@ export default function ModPackViewPage() {
return; return;
} }
const modified = new libData.ModPack(modPack.toJSON()); const modified = libData.ModPack.fromJSON(JSON.parse(JSON.stringify(modPack)));
for (let change of changes) { for (let change of changes) {
if (change.type === "name") { if (change.type === "name") {
modified.name = change.value; modified.name = change.value;
+1 -1
View File
@@ -95,7 +95,7 @@ function CreateModPackButton() {
} catch { } catch {
return; // Validation failed return; // Validation failed
} }
const modPack = new libData.ModPack(); const modPack = libData.ModPack.fromJSON({});
modPack.name = values.name; modPack.name = values.name;
modPack.factorioVersion = values.factorioVersion; modPack.factorioVersion = values.factorioVersion;
if (values.description) { modPack.description = values.description; } if (values.description) { modPack.description = values.description; }
@@ -12,7 +12,7 @@ export function useExportManifest() {
useEffect(() => { useEffect(() => {
async function load() { async function load() {
let result = await libLink.messages.getDefaultModPack.send(control); let result = await libLink.messages.getDefaultModPack.send(control);
let modPack = new libData.ModPack(result.mod_pack); let modPack = libData.ModPack.fromJSON(result.mod_pack);
if (modPack.exportManifest) { if (modPack.exportManifest) {
exportManifestCache = modPack.exportManifest; exportManifestCache = modPack.exportManifest;
setExportManifest(exportManifestCache); setExportManifest(exportManifestCache);
+2 -2
View File
@@ -12,7 +12,7 @@ export function useModPack(id) {
function updateModPack() { function updateModPack() {
libLink.messages.getModPack.send(control, { id }).then(result => { libLink.messages.getModPack.send(control, { id }).then(result => {
setModPack(new libData.ModPack(result.mod_pack)); setModPack(libData.ModPack.fromJSON(result.mod_pack));
}).catch(err => { }).catch(err => {
logger.error(`Failed to get mod pack: ${err}`); logger.error(`Failed to get mod pack: ${err}`);
setModPack({ missing: true }); setModPack({ missing: true });
@@ -41,7 +41,7 @@ export function useModPackList() {
function updateModPackList() { function updateModPackList() {
libLink.messages.listModPacks.send(control).then(result => { libLink.messages.listModPacks.send(control).then(result => {
setModPackList(result.list.map(pack => new libData.ModPack(pack))); setModPackList(result.list.map(pack => libData.ModPack.fromJSON(pack)));
}).catch(err => { }).catch(err => {
logger.error(`Failed to list mod packs:\n${err}`); logger.error(`Failed to list mod packs:\n${err}`);
}); });
+1 -1
View File
@@ -13,7 +13,7 @@ export function useModList() {
function updateModList() { function updateModList() {
libLink.messages.listMods.send(control).then(result => { libLink.messages.listMods.send(control).then(result => {
setModList(result.list.map(mod => new libData.ModInfo(mod))); setModList(result.list.map(mod => libData.ModInfo.fromJSON(mod)));
}).catch(err => { }).catch(err => {
logger.error(`Failed to list mods:\n${err}`); logger.error(`Failed to list mods:\n${err}`);
}); });
+2 -2
View File
@@ -312,7 +312,7 @@ export class Control extends libLink.Link {
} }
async modPackUpdateEventHandler(message) { async modPackUpdateEventHandler(message) {
let modPack = new libData.ModPack(message.data.mod_pack); let modPack = libData.ModPack.fromJSON(message.data.mod_pack);
let handlers = [].concat( let handlers = [].concat(
this.modPackUpdateHandlers.get(null) || [], this.modPackUpdateHandlers.get(null) || [],
this.modPackUpdateHandlers.get(modPack.id) || [], this.modPackUpdateHandlers.get(modPack.id) || [],
@@ -370,7 +370,7 @@ export class Control extends libLink.Link {
} }
async modUpdateEventHandler(message) { async modUpdateEventHandler(message) {
let mod = new libData.ModInfo(message.data.mod); let mod = libData.ModInfo.fromJSON(message.data.mod);
let handlers = [].concat( let handlers = [].concat(
this.modUpdateHandlers.get(null) || [], this.modUpdateHandlers.get(null) || [],
this.modUpdateHandlers.get(mod.name) || [] this.modUpdateHandlers.get(mod.name) || []
+4 -4
View File
@@ -308,7 +308,7 @@ describe("Integration of Clusterio", function() {
await fs.remove(exportPath); await fs.remove(exportPath);
await execCtl("instance export-data test"); await execCtl("instance export-data test");
let result = await libLink.messages.getDefaultModPack.send(getControl()); let result = await libLink.messages.getDefaultModPack.send(getControl());
let modPack = new libData.ModPack(result.mod_pack); let modPack = libData.ModPack.fromJSON(result.mod_pack);
let assets = modPack.exportManifest.assets; let assets = modPack.exportManifest.assets;
assert(Object.keys(assets).length > 1, "Export assets is empty"); assert(Object.keys(assets).length > 1, "Export assets is empty");
for (let key of ["settings", "prototypes", "item-metadata", "item-spritesheet", "locale"]) { for (let key of ["settings", "prototypes", "item-metadata", "item-spritesheet", "locale"]) {
@@ -847,7 +847,7 @@ describe("Integration of Clusterio", function() {
let response = await libLink.messages.listModPacks.send(getControl()); let response = await libLink.messages.listModPacks.send(getControl());
let modPack = response.list.find(entry => entry.name === "full-pack"); let modPack = response.list.find(entry => entry.name === "full-pack");
assert(modPack, "created mod pack not found"); assert(modPack, "created mod pack not found");
let reference = new libData.ModPack(); let reference = libData.ModPack.fromJSON({});
reference.id = modPack.id; reference.id = modPack.id;
reference.name = "full-pack"; reference.name = "full-pack";
reference.description = "Description"; reference.description = "Description";
@@ -857,7 +857,7 @@ describe("Integration of Clusterio", function() {
reference.settings["runtime-global"].set("MyInt", { value: 1235 }); reference.settings["runtime-global"].set("MyInt", { value: 1235 });
reference.settings["runtime-global"].set("MyDouble", { value: 12.25 }); reference.settings["runtime-global"].set("MyDouble", { value: 12.25 });
reference.settings["runtime-per-user"].set("MyString", { value: "a-string" }); reference.settings["runtime-per-user"].set("MyString", { value: "a-string" });
assert.deepEqual(new libData.ModPack(modPack), reference); assert.deepEqual(libData.ModPack.fromJSON(modPack), reference);
}); });
}); });
@@ -875,7 +875,7 @@ describe("Integration of Clusterio", function() {
describe("mod-pack import/export", function() { describe("mod-pack import/export", function() {
it("should should roundtrip a mod-pack", async function() { it("should should roundtrip a mod-pack", async function() {
let reference = new libData.ModPack(); let reference = libData.ModPack.fromJSON({});
reference.name = "imported-pack"; reference.name = "imported-pack";
reference.description = "Description"; reference.description = "Description";
reference.factorioVersion = "0.17.59"; reference.factorioVersion = "0.17.59";
+1 -1
View File
@@ -222,7 +222,7 @@ before(async function() {
control = new TestControl(controlConnector); control = new TestControl(controlConnector);
await controlConnector.connect(); await controlConnector.connect();
const testPack = new libData.ModPack(); const testPack = libData.ModPack.fromJSON({});
testPack.id = 12; testPack.id = 12;
testPack.name = "subspace_storage-pack"; testPack.name = "subspace_storage-pack";
testPack.factorioVersion = "1.1.0"; testPack.factorioVersion = "1.1.0";
+20 -20
View File
@@ -18,27 +18,27 @@ describe("lib/data/ModInfo", function() {
if (!validate(json)) { if (!validate(json)) {
throw validate.errors; throw validate.errors;
} }
assert.deepEqual(new ModInfo(json), pack); assert.deepEqual(ModInfo.fromJSON(json), pack);
} }
check(new ModInfo()); check(ModInfo.fromJSON({}));
check(new ModInfo({ name: "MyMod" })); check(ModInfo.fromJSON({ name: "MyMod" }));
check(new ModInfo({ version: "1.0.0" })); check(ModInfo.fromJSON({ version: "1.0.0" }));
check(new ModInfo({ title: "My Extravagent Mod" })); check(ModInfo.fromJSON({ title: "My Extravagent Mod" }));
check(new ModInfo({ author: "Me" })); check(ModInfo.fromJSON({ author: "Me" }));
check(new ModInfo({ contact: "Use my email" })); check(ModInfo.fromJSON({ contact: "Use my email" }));
check(new ModInfo({ homepage: "It's over there" })); check(ModInfo.fromJSON({ homepage: "It's over there" }));
check(new ModInfo({ description: "My Description" })); check(ModInfo.fromJSON({ description: "My Description" }));
check(new ModInfo({ factorio_version: "2.0" })); check(ModInfo.fromJSON({ factorio_version: "2.0" }));
check(new ModInfo({ dependencies: [] })); check(ModInfo.fromJSON({ dependencies: [] }));
check(new ModInfo({ dependencies: ["UltraMod", "SuperLib >= 1.00", "! bad-mod"] })); check(ModInfo.fromJSON({ dependencies: ["UltraMod", "SuperLib >= 1.00", "! bad-mod"] }));
check(new ModInfo({ filename: "MyMod_1.0.0.zip" })); check(ModInfo.fromJSON({ filename: "MyMod_1.0.0.zip" }));
check(new ModInfo({ size: 1024 })); check(ModInfo.fromJSON({ size: 1024 }));
check(new ModInfo({ sha1: "verified-as-MyMod" })); check(ModInfo.fromJSON({ sha1: "verified-as-MyMod" }));
check(new ModInfo({ is_deleted: true })); check(ModInfo.fromJSON({ is_deleted: true }));
// All at once // All at once
check(new ModInfo({ check(ModInfo.fromJSON({
name: "MyMod", name: "MyMod",
version: "1.0.0", version: "1.0.0",
title: "My Extravagent Mod", title: "My Extravagent Mod",
@@ -58,14 +58,14 @@ describe("lib/data/ModInfo", function() {
it("should sort integer mod versions lexicographically", function() { it("should sort integer mod versions lexicographically", function() {
let unsortedVersions = ["1.0.0", "1.1.0", "0.1.0", "3.0.0", "1.2.0", "0.3.1", "0.3.3", "2.1.1", "0.0.1"]; let unsortedVersions = ["1.0.0", "1.1.0", "0.1.0", "3.0.0", "1.2.0", "0.3.1", "0.3.3", "2.1.1", "0.0.1"];
let sortedVersions = ["0.0.1", "0.1.0", "0.3.1", "0.3.3", "1.0.0", "1.1.0", "1.2.0", "2.1.1", "3.0.0"]; let sortedVersions = ["0.0.1", "0.1.0", "0.3.1", "0.3.3", "1.0.0", "1.1.0", "1.2.0", "2.1.1", "3.0.0"];
let mods = unsortedVersions.map(v => new ModInfo({ version: v })); let mods = unsortedVersions.map(v => ModInfo.fromJSON({ version: v }));
mods.sort((a, b) => a.integerVersion - b.integerVersion); mods.sort((a, b) => a.integerVersion - b.integerVersion);
assert.deepEqual(mods.map(mod => mod.version), sortedVersions); assert.deepEqual(mods.map(mod => mod.version), sortedVersions);
}); });
it("should sort integer factorio versions lexicographically", function() { it("should sort integer factorio versions lexicographically", function() {
let unsortedVersions = ["1.0", "1.1", "0.1", "3.0", "1.2", "0.3", "2.1"]; let unsortedVersions = ["1.0", "1.1", "0.1", "3.0", "1.2", "0.3", "2.1"];
let sortedVersions = ["0.1", "0.3", "1.0", "1.1", "1.2", "2.1", "3.0"]; let sortedVersions = ["0.1", "0.3", "1.0", "1.1", "1.2", "2.1", "3.0"];
let factorioMods = unsortedVersions.map(v => new ModInfo({ factorio_version: v })); let factorioMods = unsortedVersions.map(v => ModInfo.fromJSON({ factorio_version: v }));
factorioMods.sort((a, b) => a.integerFactorioVersion - b.integerFactorioVersion); factorioMods.sort((a, b) => a.integerFactorioVersion - b.integerFactorioVersion);
assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions); assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions);
}); });
@@ -83,7 +83,7 @@ describe("lib/data/ModInfo", function() {
const mod = await ModInfo.fromModFile(filePath); const mod = await ModInfo.fromModFile(filePath);
assert.deepEqual( assert.deepEqual(
mod, mod,
new ModInfo({ ModInfo.fromJSON({
name: "empty_mod", name: "empty_mod",
version: "1.0.0", version: "1.0.0",
title: "An Empty Mod", title: "An Empty Mod",
+15 -15
View File
@@ -16,22 +16,22 @@ describe("lib/data/ModPack", function() {
if (!validate(json)) { if (!validate(json)) {
throw validate.errors; throw validate.errors;
} }
assert.deepEqual(new ModPack(json), pack); assert.deepEqual(ModPack.fromJSON(json), pack);
const packStringed = ModPack.fromModPackString(pack.toModPackString()); const packStringed = ModPack.fromModPackString(pack.toModPackString());
packStringed.id = pack.id; packStringed.id = pack.id;
packStringed.exportManifest = pack.exportManifest; packStringed.exportManifest = pack.exportManifest;
assert.deepEqual(packStringed, pack); assert.deepEqual(packStringed, pack);
} }
check(new ModPack()); check(ModPack.fromJSON({}));
check(new ModPack({ name: "MyPack" })); check(ModPack.fromJSON({ name: "MyPack" }));
check(new ModPack({ description: "My Description" })); check(ModPack.fromJSON({ description: "My Description" }));
check(new ModPack({ factorio_version: "2.0" })); check(ModPack.fromJSON({ factorio_version: "2.0" }));
check(new ModPack({ mods: [ check(ModPack.fromJSON({ mods: [
{ name: "subspace_storage", enabled: true, version: "1.99.8" }, { name: "subspace_storage", enabled: true, version: "1.99.8" },
{ name: "clusterio_lib", enabled: true, version: "0.1.2", sha1: "012345abcd" }, { name: "clusterio_lib", enabled: true, version: "0.1.2", sha1: "012345abcd" },
]})); ]}));
check(new ModPack({ settings: { check(ModPack.fromJSON({ settings: {
"startup": { "startup": {
"bool-setting": { "value": true }, "bool-setting": { "value": true },
}, },
@@ -42,9 +42,9 @@ describe("lib/data/ModPack", function() {
"string-setting": { "value": "a string" }, "string-setting": { "value": "a string" },
}, },
}})); }}));
check(new ModPack({ export_manifest: { assets: { setting: "settings.json" }}})); check(ModPack.fromJSON({ export_manifest: { assets: { setting: "settings.json" }}}));
check(new ModPack({ deleted: true })); check(ModPack.fromJSON({ deleted: true }));
check(new ModPack({ check(ModPack.fromJSON({
name: "Super pack", name: "Super pack",
description: "Every option at once.", description: "Every option at once.",
factorio_version: "2.0", factorio_version: "2.0",
@@ -71,7 +71,7 @@ describe("lib/data/ModPack", function() {
it("should sort integer factorio versions lexicographically", function() { it("should sort integer factorio versions lexicographically", function() {
let unsortedVersions = ["1.0", "1.1.0", "0.1", "3.0.0", "1.2", "0.3.1", "0.3.3", "2.1.1", "0.0.1"]; let unsortedVersions = ["1.0", "1.1.0", "0.1", "3.0.0", "1.2", "0.3.1", "0.3.3", "2.1.1", "0.0.1"];
let sortedVersions = ["0.0.1", "0.1", "0.3.1", "0.3.3", "1.0", "1.1.0", "1.2", "2.1.1", "3.0.0"]; let sortedVersions = ["0.0.1", "0.1", "0.3.1", "0.3.3", "1.0", "1.1.0", "1.2", "2.1.1", "3.0.0"];
let factorioMods = unsortedVersions.map(v => new ModPack({ factorio_version: v })); let factorioMods = unsortedVersions.map(v => ModPack.fromJSON({ factorio_version: v }));
factorioMods.sort((a, b) => a.integerFactorioVersion - b.integerFactorioVersion); factorioMods.sort((a, b) => a.integerFactorioVersion - b.integerFactorioVersion);
assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions); assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions);
}); });
@@ -105,7 +105,7 @@ describe("lib/data/ModPack", function() {
}; };
const mockLogger = { warn: () => {} }; const mockLogger = { warn: () => {} };
it("should fill in defaults for settings", function() { it("should fill in defaults for settings", function() {
const pack = new ModPack(); const pack = ModPack.fromJSON({});
pack.fillDefaultSettings(prototypes, mockLogger); pack.fillDefaultSettings(prototypes, mockLogger);
assert.deepEqual(pack.toJSON().settings, { assert.deepEqual(pack.toJSON().settings, {
"startup": { "startup": {
@@ -120,7 +120,7 @@ describe("lib/data/ModPack", function() {
}); });
}); });
it("should not overwrite existing values", function() { it("should not overwrite existing values", function() {
const pack = new ModPack({ settings: { const pack = ModPack.fromJSON({ settings: {
"startup": { "startup": {
"bool": { "value": false }, "bool": { "value": false },
}, },
@@ -145,7 +145,7 @@ describe("lib/data/ModPack", function() {
}); });
}); });
it("should ignore unknown setting_type and missing default_value", function() { it("should ignore unknown setting_type and missing default_value", function() {
const pack = new ModPack(); const pack = ModPack.fromJSON({});
pack.fillDefaultSettings({ pack.fillDefaultSettings({
"string-setting": { "string-setting": {
"foo": { "foo": {
@@ -196,7 +196,7 @@ describe("lib/data/ModPack", function() {
describe("toModSettingsDat()", function() { describe("toModSettingsDat()", function() {
it("should properly serialise settings", function() { it("should properly serialise settings", function() {
const pack = new ModPack({ settings: { const pack = ModPack.fromJSON({ settings: {
"startup": { "startup": {
"bool-setting": { "value": true }, "bool-setting": { "value": true },
}, },