Skip to content

Commit

Permalink
adds support for setting multiple variables to varSet (#100458) (#102136
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ppisljar committed Jun 14, 2021
1 parent e0f2ed9 commit ec5c0d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
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;
},
};

0 comments on commit ec5c0d3

Please sign in to comment.