Skip to content

Commit

Permalink
make switchBy work with non string values (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored Dec 16, 2024
1 parent 2f06afe commit 82d2179
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silly-students-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dmno/configraph": patch
---

make switchBy work with non string values
2 changes: 1 addition & 1 deletion packages/configraph/src/resolvers/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 17 additions & 0 deletions packages/configraph/test/resolution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 82d2179

Please sign in to comment.