Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Expressions] Add support of partial results to the switch expression function #108086

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,55 @@ describe('switch', () => {
expectObservable(fn(context, args)).toBe('(0|)', [result])
);
});

it('should support partial results', () => {
testScheduler.run(({ cold, expectObservable }) => {
const context = 'foo';
const case1 = cold('--ab-c-', {
a: {
type: 'case',
matches: false,
result: 1,
},
b: {
type: 'case',
matches: true,
result: 2,
},
c: {
type: 'case',
matches: false,
result: 3,
},
});
const case2 = cold('-a--bc-', {
a: {
type: 'case',
matches: true,
result: 4,
},
b: {
type: 'case',
matches: true,
result: 5,
},
c: {
type: 'case',
matches: true,
result: 6,
},
});
const expected = ' --abc(de)-';
const args = { case: [() => case1, () => case2] };
expectObservable(fn(context, args)).toBe(expected, {
a: 4,
b: 2,
c: 2,
d: 5,
e: 6,
});
});
});
});
});
});
20 changes: 12 additions & 8 deletions x-pack/plugins/canvas/canvas_plugin_src/functions/common/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { Observable, defer, from, of } from 'rxjs';
import { concatMap, filter, merge, pluck, take } from 'rxjs/operators';
import { Observable, combineLatest, defer, of } from 'rxjs';
import { concatMap } from 'rxjs/operators';
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { Case } from '../../../types';
import { getFunctionHelp } from '../../../i18n';
Expand Down Expand Up @@ -43,12 +43,16 @@ export function switchFn(): ExpressionFunctionDefinition<
},
},
fn(input, args) {
return from(args.case ?? []).pipe(
concatMap((item) => item()),
filter(({ matches }) => matches),
pluck('result'),
merge(defer(() => args.default?.() ?? of(input))),
take(1)
const cases = args.case?.map((item) => defer(() => item())) ?? [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Expressions spec above, case is a required argument. I'm curious why the Argument interface was changed in #100409 and if/why it needs to remain so.

@ppisljar I thought the types I had written in Canvas would through a TS error if required was true and the argument was optional... I may be misremembering, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good finding. I've corrected types because we had that check in the implementation and, for some reason, test cases for that. I remember @ppisljar asked me to correct all the optional types, but it seems like I forgot to correct this one here.
Please see my changes in the latest commit.

const cases$ = cases.length ? combineLatest(cases) : of([]);

return cases$.pipe(
concatMap((items) => {
const item = items.find(({ matches }) => matches);
const item$ = item && of(item.result);

return item$ ?? args.default?.() ?? of(input);
})
);
},
};
Expand Down