diff --git a/client/config.test.ts b/client/config.test.ts index f0493d47..7a91d1ae 100644 --- a/client/config.test.ts +++ b/client/config.test.ts @@ -8,6 +8,11 @@ Deno.test("Config - basic functionality", () => { config.set("testKey", "testValue"); assertEquals(config.get(["testKey"], null), "testValue"); + // Test insert + config.insert("plugs", "plug1"); + config.insert("plugs", "plug1"); + assertEquals(config.get("plugs", null), ["plug1", "plug1"]); + // Test default value assertEquals(config.get("nonExistentKey", "default"), "default"); diff --git a/client/config.ts b/client/config.ts index 8c17fbca..45ff826a 100644 --- a/client/config.ts +++ b/client/config.ts @@ -134,6 +134,27 @@ export class Config { } } + insert( + key: string | string[], + value?: T, + ): void { + if (typeof key === "string") { + key = key.split("."); + } + const resolved = resolvePath(this.values, key, true); + if (resolved) { + if (!Array.isArray(resolved.obj[resolved.key])) { + resolved.obj[resolved.key] = []; + } + resolved.obj[resolved.key].push(value); + } else { + throw new Error(`Invalid key ${key}`); + } + + // Find and validate only the relevant schema after the fact... + this.validatePath(key); + } + /** * Checks if a path exists in the config * @param path The path to check, supports dot notation (e.g. "foo.bar.baz") diff --git a/client/plugos/syscalls/config.ts b/client/plugos/syscalls/config.ts index eff7ac79..1793ac48 100644 --- a/client/plugos/syscalls/config.ts +++ b/client/plugos/syscalls/config.ts @@ -13,6 +13,13 @@ export function configSyscalls(config: Config): SysCallMapping { ) => { config.set(keyOrValues as any, value); }, + "config.insert": ( + _ctx, + key: string | string[], + value: any, + ) => { + config.insert(key, value); + }, "config.has": (_ctx, path: string) => { return config.has(path); }, diff --git a/plug-api/syscalls/config.ts b/plug-api/syscalls/config.ts index ad6865da..01ba662f 100644 --- a/plug-api/syscalls/config.ts +++ b/plug-api/syscalls/config.ts @@ -28,6 +28,16 @@ export function set( return syscall("config.set", pathOrValues, value); } +/** + * Inserts a config value into an array + */ +export function insert( + path: string | string[], + value: T, +): Promise { + return syscall("config.insert", path, value); +} + /** * Checks if a config path exists. * @param path The path to check