mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 07:30:41 +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
@@ -308,7 +308,7 @@ describe("Integration of Clusterio", function() {
|
||||
await fs.remove(exportPath);
|
||||
await execCtl("instance export-data test");
|
||||
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;
|
||||
assert(Object.keys(assets).length > 1, "Export assets is empty");
|
||||
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 modPack = response.list.find(entry => entry.name === "full-pack");
|
||||
assert(modPack, "created mod pack not found");
|
||||
let reference = new libData.ModPack();
|
||||
let reference = libData.ModPack.fromJSON({});
|
||||
reference.id = modPack.id;
|
||||
reference.name = "full-pack";
|
||||
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("MyDouble", { value: 12.25 });
|
||||
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() {
|
||||
it("should should roundtrip a mod-pack", async function() {
|
||||
let reference = new libData.ModPack();
|
||||
let reference = libData.ModPack.fromJSON({});
|
||||
reference.name = "imported-pack";
|
||||
reference.description = "Description";
|
||||
reference.factorioVersion = "0.17.59";
|
||||
|
||||
@@ -222,7 +222,7 @@ before(async function() {
|
||||
control = new TestControl(controlConnector);
|
||||
await controlConnector.connect();
|
||||
|
||||
const testPack = new libData.ModPack();
|
||||
const testPack = libData.ModPack.fromJSON({});
|
||||
testPack.id = 12;
|
||||
testPack.name = "subspace_storage-pack";
|
||||
testPack.factorioVersion = "1.1.0";
|
||||
|
||||
+20
-20
@@ -18,27 +18,27 @@ describe("lib/data/ModInfo", function() {
|
||||
if (!validate(json)) {
|
||||
throw validate.errors;
|
||||
}
|
||||
assert.deepEqual(new ModInfo(json), pack);
|
||||
assert.deepEqual(ModInfo.fromJSON(json), pack);
|
||||
}
|
||||
|
||||
check(new ModInfo());
|
||||
check(new ModInfo({ name: "MyMod" }));
|
||||
check(new ModInfo({ version: "1.0.0" }));
|
||||
check(new ModInfo({ title: "My Extravagent Mod" }));
|
||||
check(new ModInfo({ author: "Me" }));
|
||||
check(new ModInfo({ contact: "Use my email" }));
|
||||
check(new ModInfo({ homepage: "It's over there" }));
|
||||
check(new ModInfo({ description: "My Description" }));
|
||||
check(new ModInfo({ factorio_version: "2.0" }));
|
||||
check(new ModInfo({ dependencies: [] }));
|
||||
check(new ModInfo({ dependencies: ["UltraMod", "SuperLib >= 1.00", "! bad-mod"] }));
|
||||
check(new ModInfo({ filename: "MyMod_1.0.0.zip" }));
|
||||
check(new ModInfo({ size: 1024 }));
|
||||
check(new ModInfo({ sha1: "verified-as-MyMod" }));
|
||||
check(new ModInfo({ is_deleted: true }));
|
||||
check(ModInfo.fromJSON({}));
|
||||
check(ModInfo.fromJSON({ name: "MyMod" }));
|
||||
check(ModInfo.fromJSON({ version: "1.0.0" }));
|
||||
check(ModInfo.fromJSON({ title: "My Extravagent Mod" }));
|
||||
check(ModInfo.fromJSON({ author: "Me" }));
|
||||
check(ModInfo.fromJSON({ contact: "Use my email" }));
|
||||
check(ModInfo.fromJSON({ homepage: "It's over there" }));
|
||||
check(ModInfo.fromJSON({ description: "My Description" }));
|
||||
check(ModInfo.fromJSON({ factorio_version: "2.0" }));
|
||||
check(ModInfo.fromJSON({ dependencies: [] }));
|
||||
check(ModInfo.fromJSON({ dependencies: ["UltraMod", "SuperLib >= 1.00", "! bad-mod"] }));
|
||||
check(ModInfo.fromJSON({ filename: "MyMod_1.0.0.zip" }));
|
||||
check(ModInfo.fromJSON({ size: 1024 }));
|
||||
check(ModInfo.fromJSON({ sha1: "verified-as-MyMod" }));
|
||||
check(ModInfo.fromJSON({ is_deleted: true }));
|
||||
|
||||
// All at once
|
||||
check(new ModInfo({
|
||||
check(ModInfo.fromJSON({
|
||||
name: "MyMod",
|
||||
version: "1.0.0",
|
||||
title: "My Extravagent Mod",
|
||||
@@ -58,14 +58,14 @@ describe("lib/data/ModInfo", 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 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);
|
||||
assert.deepEqual(mods.map(mod => mod.version), sortedVersions);
|
||||
});
|
||||
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 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);
|
||||
assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions);
|
||||
});
|
||||
@@ -83,7 +83,7 @@ describe("lib/data/ModInfo", function() {
|
||||
const mod = await ModInfo.fromModFile(filePath);
|
||||
assert.deepEqual(
|
||||
mod,
|
||||
new ModInfo({
|
||||
ModInfo.fromJSON({
|
||||
name: "empty_mod",
|
||||
version: "1.0.0",
|
||||
title: "An Empty Mod",
|
||||
|
||||
+15
-15
@@ -16,22 +16,22 @@ describe("lib/data/ModPack", function() {
|
||||
if (!validate(json)) {
|
||||
throw validate.errors;
|
||||
}
|
||||
assert.deepEqual(new ModPack(json), pack);
|
||||
assert.deepEqual(ModPack.fromJSON(json), pack);
|
||||
const packStringed = ModPack.fromModPackString(pack.toModPackString());
|
||||
packStringed.id = pack.id;
|
||||
packStringed.exportManifest = pack.exportManifest;
|
||||
assert.deepEqual(packStringed, pack);
|
||||
}
|
||||
|
||||
check(new ModPack());
|
||||
check(new ModPack({ name: "MyPack" }));
|
||||
check(new ModPack({ description: "My Description" }));
|
||||
check(new ModPack({ factorio_version: "2.0" }));
|
||||
check(new ModPack({ mods: [
|
||||
check(ModPack.fromJSON({}));
|
||||
check(ModPack.fromJSON({ name: "MyPack" }));
|
||||
check(ModPack.fromJSON({ description: "My Description" }));
|
||||
check(ModPack.fromJSON({ factorio_version: "2.0" }));
|
||||
check(ModPack.fromJSON({ mods: [
|
||||
{ name: "subspace_storage", enabled: true, version: "1.99.8" },
|
||||
{ name: "clusterio_lib", enabled: true, version: "0.1.2", sha1: "012345abcd" },
|
||||
]}));
|
||||
check(new ModPack({ settings: {
|
||||
check(ModPack.fromJSON({ settings: {
|
||||
"startup": {
|
||||
"bool-setting": { "value": true },
|
||||
},
|
||||
@@ -42,9 +42,9 @@ describe("lib/data/ModPack", function() {
|
||||
"string-setting": { "value": "a string" },
|
||||
},
|
||||
}}));
|
||||
check(new ModPack({ export_manifest: { assets: { setting: "settings.json" }}}));
|
||||
check(new ModPack({ deleted: true }));
|
||||
check(new ModPack({
|
||||
check(ModPack.fromJSON({ export_manifest: { assets: { setting: "settings.json" }}}));
|
||||
check(ModPack.fromJSON({ deleted: true }));
|
||||
check(ModPack.fromJSON({
|
||||
name: "Super pack",
|
||||
description: "Every option at once.",
|
||||
factorio_version: "2.0",
|
||||
@@ -71,7 +71,7 @@ describe("lib/data/ModPack", 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 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);
|
||||
assert.deepEqual(factorioMods.map(mod => mod.factorioVersion), sortedVersions);
|
||||
});
|
||||
@@ -105,7 +105,7 @@ describe("lib/data/ModPack", function() {
|
||||
};
|
||||
const mockLogger = { warn: () => {} };
|
||||
it("should fill in defaults for settings", function() {
|
||||
const pack = new ModPack();
|
||||
const pack = ModPack.fromJSON({});
|
||||
pack.fillDefaultSettings(prototypes, mockLogger);
|
||||
assert.deepEqual(pack.toJSON().settings, {
|
||||
"startup": {
|
||||
@@ -120,7 +120,7 @@ describe("lib/data/ModPack", function() {
|
||||
});
|
||||
});
|
||||
it("should not overwrite existing values", function() {
|
||||
const pack = new ModPack({ settings: {
|
||||
const pack = ModPack.fromJSON({ settings: {
|
||||
"startup": {
|
||||
"bool": { "value": false },
|
||||
},
|
||||
@@ -145,7 +145,7 @@ describe("lib/data/ModPack", function() {
|
||||
});
|
||||
});
|
||||
it("should ignore unknown setting_type and missing default_value", function() {
|
||||
const pack = new ModPack();
|
||||
const pack = ModPack.fromJSON({});
|
||||
pack.fillDefaultSettings({
|
||||
"string-setting": {
|
||||
"foo": {
|
||||
@@ -196,7 +196,7 @@ describe("lib/data/ModPack", function() {
|
||||
|
||||
describe("toModSettingsDat()", function() {
|
||||
it("should properly serialise settings", function() {
|
||||
const pack = new ModPack({ settings: {
|
||||
const pack = ModPack.fromJSON({ settings: {
|
||||
"startup": {
|
||||
"bool-setting": { "value": true },
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user