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

+4 -4
View File
@@ -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";
+1 -1
View File
@@ -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";