mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-25 16:33:12 +09:00
Implement setProp in configs
Add setProp method for selectively changing one property of an object field in a config group.
This commit is contained in:
1 parent
8c9daff0bb
commit
5b043524b4
2 files changed
+106
-28
No files matched your search
+44
-1
@@ -164,6 +164,14 @@ class Config extends events.EventEmitter {
|
|||||||
return this.group(group).set(field, value);
|
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
|
* 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
|
* The field must be defined for the config group and the value is
|
||||||
* type checked against the field definition.
|
* 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.
|
* @throws {module:lib/config.InvalidValue} if value is not allowed for the field.
|
||||||
*/
|
*/
|
||||||
set(name, value) {
|
set(name, value) {
|
||||||
let def = this.constructor._definitions.get(name);
|
let def = this.constructor._definitions.get(name);
|
||||||
if (!def) {
|
if (!def) {
|
||||||
throw new Error(`No field named '${name}'`);
|
throw new InvalidField(`No field named '${name}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty strings are treates as null
|
// 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
|
* Update a config group from a serialized group
|
||||||
*
|
*
|
||||||
|
|||||||
+62
-27
@@ -73,7 +73,7 @@ describe("lib/config/classes", function() {
|
|||||||
TestGroup.define({ name: "test", type: "string", optional: true });
|
TestGroup.define({ name: "test", type: "string", optional: true });
|
||||||
TestGroup.define({ name: "func", type: "number", initial_value: () => 42 });
|
TestGroup.define({ name: "func", type: "number", initial_value: () => 42 });
|
||||||
TestGroup.define({ name: "bool", type: "boolean", initial_value: false, optional: true });
|
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("test"), "field test was not defined");
|
||||||
assert(TestGroup._definitions.has("func"), "field func 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() {
|
describe(".set()", function() {
|
||||||
let testInstance;
|
let testInstance;
|
||||||
before(function() {
|
before(async function() {
|
||||||
testInstance = new TestGroup();
|
testInstance = new TestGroup();
|
||||||
testInstance.update({ name: "test_group", fields: {
|
await testInstance.load(null, { name: "test_group", fields: {
|
||||||
enum: "a", test: "blah", func: 27,
|
enum: "a", test: "blah", func: 27,
|
||||||
}}, false);
|
}});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should throw if field does not exist", function() {
|
it("should throw if field does not exist", function() {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => testInstance.set("bar", 1),
|
() => 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() {
|
describe("Config", function() {
|
||||||
@@ -372,7 +408,7 @@ describe("lib/config/classes", function() {
|
|||||||
AlphaGroup.define({ name: "foo", type: "string", optional: true });
|
AlphaGroup.define({ name: "foo", type: "string", optional: true });
|
||||||
AlphaGroup.finalize();
|
AlphaGroup.finalize();
|
||||||
TestConfig.registerGroup(AlphaGroup);
|
TestConfig.registerGroup(AlphaGroup);
|
||||||
BetaGroup.define({ name: "bar", type: "number", initial_value: 2 });
|
BetaGroup.define({ name: "bar", type: "object", initial_value: {} });
|
||||||
BetaGroup.finalize();
|
BetaGroup.finalize();
|
||||||
TestConfig.registerGroup(BetaGroup);
|
TestConfig.registerGroup(BetaGroup);
|
||||||
});
|
});
|
||||||
@@ -424,7 +460,7 @@ describe("lib/config/classes", function() {
|
|||||||
let testInstance = new TestConfig();
|
let testInstance = new TestConfig();
|
||||||
await testInstance.init();
|
await testInstance.init();
|
||||||
assert.equal(testInstance.get("alpha.foo"), null);
|
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",
|
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" }}] });
|
await testInstance.load({ groups: [{ name: "alpha", fields: { foo: "a" }}] });
|
||||||
|
|
||||||
assert.equal(testInstance.get("alpha.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() {
|
it("should preserve unknown groups when serialized back", async function() {
|
||||||
@@ -484,7 +520,7 @@ describe("lib/config/classes", function() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "beta",
|
name: "beta",
|
||||||
fields: { bar: 20 },
|
fields: { bar: { value: 20 }},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -506,7 +542,7 @@ describe("lib/config/classes", function() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "beta",
|
name: "beta",
|
||||||
fields: { bar: 30 },
|
fields: { bar: { value: 30 }},
|
||||||
}
|
}
|
||||||
]}, false);
|
]}, false);
|
||||||
|
|
||||||
@@ -522,7 +558,7 @@ describe("lib/config/classes", function() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "beta",
|
name: "beta",
|
||||||
fields: { bar: 30 },
|
fields: { bar: { value: 30 }},
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -572,33 +608,32 @@ describe("lib/config/classes", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("fieldChanged event", function() {
|
describe("fieldChanged event", function() {
|
||||||
it("should be called when setting a field", async function() {
|
let testInstance;
|
||||||
let testInstance = new TestConfig();
|
let called;
|
||||||
|
beforeEach(async function() {
|
||||||
|
testInstance = new TestConfig();
|
||||||
await testInstance.init();
|
await testInstance.init();
|
||||||
|
|
||||||
let called = false;
|
called = false;
|
||||||
testInstance.once("fieldChanged", (group, field, prev) => {
|
testInstance.once("fieldChanged", (group, field, prev) => {
|
||||||
if (group.constructor.groupName === "alpha" && field === "foo") {
|
if (group.name === "beta" && field === "bar") {
|
||||||
called = true;
|
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");
|
assert(called, "fieldChanged was not called");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be called when updating a config", async function() {
|
it("should be called when updating a config", async function() {
|
||||||
let testInstance = new TestConfig();
|
testInstance.update({ groups: [{ name: "beta", fields: { bar: { value: 1 }}}]}, true);
|
||||||
await testInstance.init();
|
assert(called, "fieldChanged was not called");
|
||||||
|
});
|
||||||
|
|
||||||
let called = false;
|
it("should be called when setting a prop", async function() {
|
||||||
testInstance.once("fieldChanged", (group, field, prev) => {
|
testInstance.setProp("beta.bar", "value", 2);
|
||||||
if (group.constructor.groupName === "alpha" && field === "foo") {
|
|
||||||
called = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
testInstance.update({ groups: [{ name: "alpha", fields: { foo: "new value" }}]}, true);
|
|
||||||
assert(called, "fieldChanged was not called");
|
assert(called, "fieldChanged was not called");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user