From 82d21790f90dc7d64033a28368cdae3a860b1144 Mon Sep 17 00:00:00 2001 From: Theo Ephraim Date: Mon, 16 Dec 2024 12:45:21 -0800 Subject: [PATCH] make switchBy work with non string values (#183) --- .changeset/silly-students-talk.md | 5 +++++ packages/configraph/src/resolvers/switch.ts | 2 +- packages/configraph/test/resolution.test.ts | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/silly-students-talk.md diff --git a/.changeset/silly-students-talk.md b/.changeset/silly-students-talk.md new file mode 100644 index 00000000..bb32d171 --- /dev/null +++ b/.changeset/silly-students-talk.md @@ -0,0 +1,5 @@ +--- +"@dmno/configraph": patch +--- + +make switchBy work with non string values diff --git a/packages/configraph/src/resolvers/switch.ts b/packages/configraph/src/resolvers/switch.ts index 63d620ef..8ddc301e 100644 --- a/packages/configraph/src/resolvers/switch.ts +++ b/packages/configraph/src/resolvers/switch.ts @@ -21,7 +21,7 @@ export function switchBy(switchByKey: string, branches: SwitchByResolverOptions) resolveBranches: _.map(branches, (itemDef, itemKey) => ({ // TODO: do we want to use a special symbol? or pass default as different arg? isDefault: itemKey === '_default' || itemKey === '_', - condition: (ctx: ResolverContext) => ctx.get(switchByKey) === itemKey, + condition: (ctx: ResolverContext) => ctx.get(switchByKey)?.toString() === itemKey, id: itemKey, label: `${switchByKey} === "${itemKey}"`, resolver: processInlineResolverDef(itemDef), diff --git a/packages/configraph/test/resolution.test.ts b/packages/configraph/test/resolution.test.ts index 47e08d09..20408a24 100644 --- a/packages/configraph/test/resolution.test.ts +++ b/packages/configraph/test/resolution.test.ts @@ -143,6 +143,23 @@ describe('graph resolution', () => { expect(e.configNodes.switchTest.resolvedValue).toBe('default-val'); }); + test('works with non-string values', async () => { + const g = new Configraph(); + const e = g.createEntity({ + configSchema: { + someBool: { value: true }, + switchTest: { + value: switchBy('someBool', { + true: 'true-val', + false: 'false-val', + }), + }, + }, + }); + await g.resolveConfig(); + expect(e.configNodes.switchTest.resolvedValue).toBe('true-val'); + }); + test('item has a SchemaError if key/path is not valid', async () => { const g = new Configraph(); const e = g.createEntity({