From 5b043524b4b35580d1bb3167303b12b123b42eb8 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 31 May 2020 16:08:21 +0200 Subject: [PATCH] Implement setProp in configs Add setProp method for selectively changing one property of an object field in a config group. --- lib/config/classes.js | 45 ++++++++++++++++++- test/lib/config/classes.js | 89 ++++++++++++++++++++++++++------------ 2 files changed, 106 insertions(+), 28 deletions(-) diff --git a/lib/config/classes.js b/lib/config/classes.js index 14ee1b8b..1e3d1c39 100644 --- a/lib/config/classes.js +++ b/lib/config/classes.js @@ -164,6 +164,14 @@ class Config extends events.EventEmitter { return this.group(group).set(field, value); } + /** + * Set property of an object value for a config field + */ + setProp(name, prop, value) { + let [group, field] = splitOn('.', name); + return this.group(group).setProp(field, prop, value); + } + /** * Get the config group instance with the given name */ @@ -327,12 +335,13 @@ class ConfigGroup { * The field must be defined for the config group and the value is * type checked against the field definition. * + * @throws {module:lib/config.InvalidField} if field is not defined. * @throws {module:lib/config.InvalidValue} if value is not allowed for the field. */ set(name, value) { let def = this.constructor._definitions.get(name); if (!def) { - throw new Error(`No field named '${name}'`); + throw new InvalidField(`No field named '${name}'`); } // Empty strings are treates as null @@ -382,6 +391,40 @@ class ConfigGroup { } } + /** + * Set property of object field + * + * Update value of stored object field by setting the specified property + * on it. The field must be defined as an object for the config group. + * + * @throws {module:lib/config.InvalidField} if field is not defined. + * @throws {module:lib/config.InvalidValue} if field is not an object. + */ + setProp(name, prop, value) { + let def = this.constructor._definitions.get(name); + if (!def) { + throw new InvalidField(`No field named '${name}'`); + } + + if (def.type !== "object") { + throw new InvalidValue(`Cannot set property on non-object field '${name}'`); + } + + let prev = this._fields.get(name); + let updated = {}; + if (prev !== null) { + for (let [key, value] of Object.entries(prev)) { + updated[key] = value; + } + } + + updated[prop] = value; + this._fields.set(name, updated); + if (this._config && !util.isDeepStrictEqual(updated, prev)) { + this._config.emit("fieldChanged", this, name, prev); + } + } + /** * Update a config group from a serialized group * diff --git a/test/lib/config/classes.js b/test/lib/config/classes.js index 614a020b..e094e4a2 100644 --- a/test/lib/config/classes.js +++ b/test/lib/config/classes.js @@ -73,7 +73,7 @@ describe("lib/config/classes", function() { TestGroup.define({ name: "test", type: "string", optional: true }); TestGroup.define({ name: "func", type: "number", initial_value: () => 42 }); TestGroup.define({ name: "bool", type: "boolean", initial_value: false, optional: true }); - TestGroup.define({ name: "json", type: "object", initial_value: {} }); + TestGroup.define({ name: "json", type: "object", initial_value: {}, optional: true }); assert(TestGroup._definitions.has("test"), "field test was not defined"); assert(TestGroup._definitions.has("func"), "field func was not defined"); @@ -265,17 +265,17 @@ describe("lib/config/classes", function() { describe(".set()", function() { let testInstance; - before(function() { + before(async function() { testInstance = new TestGroup(); - testInstance.update({ name: "test_group", fields: { + await testInstance.load(null, { name: "test_group", fields: { enum: "a", test: "blah", func: 27, - }}, false); + }}); }); it("should throw if field does not exist", function() { assert.throws( () => testInstance.set("bar", 1), - new Error("No field named 'bar'") + new classes.InvalidField("No field named 'bar'") ); }) @@ -359,6 +359,42 @@ describe("lib/config/classes", function() { ); }); }); + + describe(".setProp()", function() { + let testInstance; + before(async function() { + testInstance = new TestGroup(); + await testInstance.load(null, { name: "test_group", fields: { + enum: "a", test: "blah", func: 27, + }}); + }); + + it("should throw if field does not exist", function() { + assert.throws( + () => testInstance.setProp("bar", 1), + new classes.InvalidField("No field named 'bar'") + ); + }) + + it("should throw if field is not an object", function() { + assert.throws( + () => testInstance.setProp("enum", "a"), + new classes.InvalidField("Cannot set property on non-object field 'enum'") + ); + }) + + it("should work if field is an object", function() { + testInstance.set("json", { prev: 32, test: false }); + testInstance.setProp("json", "test", true); + assert.deepEqual(testInstance.get("json"), { prev: 32, test: true }); + }); + + it("should handle field being null", function() { + testInstance.set("json", null); + testInstance.setProp("json", "test", true); + assert.deepEqual(testInstance.get("json"), { test: true }); + }); + }); }); describe("Config", function() { @@ -372,7 +408,7 @@ describe("lib/config/classes", function() { AlphaGroup.define({ name: "foo", type: "string", optional: true }); AlphaGroup.finalize(); TestConfig.registerGroup(AlphaGroup); - BetaGroup.define({ name: "bar", type: "number", initial_value: 2 }); + BetaGroup.define({ name: "bar", type: "object", initial_value: {} }); BetaGroup.finalize(); TestConfig.registerGroup(BetaGroup); }); @@ -424,7 +460,7 @@ describe("lib/config/classes", function() { let testInstance = new TestConfig(); await testInstance.init(); assert.equal(testInstance.get("alpha.foo"), null); - assert.equal(testInstance.get("beta.bar"), 2); + assert.deepEqual(testInstance.get("beta.bar"), {}); }) }); @@ -440,7 +476,7 @@ describe("lib/config/classes", function() { }, { name: "beta", - fields: { bar: 2 }, + fields: { bar: {} }, } ] }); @@ -469,7 +505,7 @@ describe("lib/config/classes", function() { await testInstance.load({ groups: [{ name: "alpha", fields: { foo: "a" }}] }); assert.equal(testInstance.get("alpha.foo"), "a"); - assert.equal(testInstance.get("beta.bar"), 2); + assert.deepEqual(testInstance.get("beta.bar"), {}); }); it("should preserve unknown groups when serialized back", async function() { @@ -484,7 +520,7 @@ describe("lib/config/classes", function() { }, { name: "beta", - fields: { bar: 20 }, + fields: { bar: { value: 20 }}, } ] @@ -506,7 +542,7 @@ describe("lib/config/classes", function() { }, { name: "beta", - fields: { bar: 30 }, + fields: { bar: { value: 30 }}, } ]}, false); @@ -522,7 +558,7 @@ describe("lib/config/classes", function() { }, { name: "beta", - fields: { bar: 30 }, + fields: { bar: { value: 30 }}, } ], }); @@ -572,33 +608,32 @@ describe("lib/config/classes", function() { }); describe("fieldChanged event", function() { - it("should be called when setting a field", async function() { - let testInstance = new TestConfig(); + let testInstance; + let called; + beforeEach(async function() { + testInstance = new TestConfig(); await testInstance.init(); - let called = false; + called = false; testInstance.once("fieldChanged", (group, field, prev) => { - if (group.constructor.groupName === "alpha" && field === "foo") { + if (group.name === "beta" && field === "bar") { called = true; } }); + }); - testInstance.set("alpha.foo", "new value"); + it("should be called when setting a field", async function() { + testInstance.set("beta.bar", { value: 1 }); assert(called, "fieldChanged was not called"); }); it("should be called when updating a config", async function() { - let testInstance = new TestConfig(); - await testInstance.init(); + testInstance.update({ groups: [{ name: "beta", fields: { bar: { value: 1 }}}]}, true); + assert(called, "fieldChanged was not called"); + }); - let called = false; - testInstance.once("fieldChanged", (group, field, prev) => { - if (group.constructor.groupName === "alpha" && field === "foo") { - called = true; - } - }); - - testInstance.update({ groups: [{ name: "alpha", fields: { foo: "new value" }}]}, true); + it("should be called when setting a prop", async function() { + testInstance.setProp("beta.bar", "value", 2); assert(called, "fieldChanged was not called"); }); });