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

adds support for setting multiple variables to varSet #100458

Merged
merged 6 commits into from Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/plugins/expressions/common/execution/execution.test.ts
Expand Up @@ -834,8 +834,8 @@ describe('Execution', () => {

expect((chain[0].arguments.val[0] as ExpressionAstExpression).chain[0].debug!.args).toEqual(
{
name: 'foo',
value: 5,
name: ['foo'],
value: [5],
}
);
});
Expand Down
Expand Up @@ -9,6 +9,8 @@
import { functionWrapper } from './utils';
import { variableSet } from '../var_set';
import { ExecutionContext } from '../../../execution/types';
import { createUnitTestExecutor } from '../../../test_helpers';
import { first } from 'rxjs/operators';

describe('expression_functions', () => {
describe('var_set', () => {
Expand All @@ -32,21 +34,49 @@ describe('expression_functions', () => {
});

it('updates a variable', () => {
const actual = fn(input, { name: 'test', value: 2 }, context);
const actual = fn(input, { name: ['test'], value: [2] }, context);
expect(variables.test).toEqual(2);
expect(actual).toEqual(input);
});

it('sets a new variable', () => {
const actual = fn(input, { name: 'new', value: 3 }, context);
const actual = fn(input, { name: ['new'], value: [3] }, context);
expect(variables.new).toEqual(3);
expect(actual).toEqual(input);
});

it('stores context if value is not set', () => {
const actual = fn(input, { name: 'test' }, context);
const actual = fn(input, { name: ['test'], value: [] }, context);
expect(variables.test).toEqual(input);
expect(actual).toEqual(input);
});

it('sets multiple variables', () => {
const actual = fn(input, { name: ['new1', 'new2', 'new3'], value: [1, , 3] }, context);
expect(variables.new1).toEqual(1);
expect(variables.new2).toEqual(input);
expect(variables.new3).toEqual(3);
expect(actual).toEqual(input);
});

describe('running function thru executor', () => {
const executor = createUnitTestExecutor();
executor.registerFunction(variableSet);

it('sets the variables', async () => {
const vars = {};
const result = await executor
.run('var_set name=test1 name=test2 value=1', 2, { variables: vars })
.pipe(first())
.toPromise();

expect(result).toEqual(2);

expect(vars).toEqual({
test1: 1,
test2: 2,
});
});
});
});
});
Expand Up @@ -10,8 +10,8 @@ import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../types';

interface Arguments {
name: string;
value?: any;
name: string[];
value: any[];
}

export type ExpressionFunctionVarSet = ExpressionFunctionDefinition<
Expand All @@ -31,12 +31,14 @@ export const variableSet: ExpressionFunctionVarSet = {
types: ['string'],
aliases: ['_'],
required: true,
multi: true,
help: i18n.translate('expressions.functions.varset.name.help', {
defaultMessage: 'Specify the name of the variable.',
}),
},
value: {
aliases: ['val'],
multi: true,
help: i18n.translate('expressions.functions.varset.val.help', {
defaultMessage:
'Specify the value for the variable. When unspecified, the input context is used.',
Expand All @@ -45,7 +47,9 @@ export const variableSet: ExpressionFunctionVarSet = {
},
fn(input, args, context) {
const variables: Record<string, any> = context.variables;
variables[args.name] = args.value === undefined ? input : args.value;
args.name.forEach((name, i) => {
variables[name] = args.value[i] === undefined ? input : args.value[i];
});
return input;
},
};