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

variable support for interpreter #54788

Merged
merged 3 commits into from
Jan 15, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/expressions/public/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class ExpressionDataHandler {
getInitialContext,
inspectorAdapters: this.inspectorAdapters,
abortSignal: this.abortController.signal,
variables: params.variables,
})
.then(
(v: IInterpreterResult) => {
Expand Down
63 changes: 63 additions & 0 deletions src/plugins/expressions/public/functions/tests/var.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from './utils';
import { variable } from '../var';
import { FunctionHandlers } from '../../../common/types';
import { KibanaContext } from '../../../common/expression_types/kibana_context';

describe('interpreter/functions#var', () => {
const fn = functionWrapper(variable);
let context: Partial<KibanaContext>;
let initialContext: KibanaContext;
let handlers: FunctionHandlers;

beforeEach(() => {
context = { timeRange: { from: '0', to: '1' } };
initialContext = {
type: 'kibana_context',
query: { language: 'lucene', query: 'geo.src:US' },
filters: [
{
meta: {
disabled: false,
negate: false,
alias: null,
},
query: { match: {} },
},
],
timeRange: { from: '2', to: '3' },
};
handlers = {
getInitialContext: () => initialContext,
variables: { test: 1 } as any,
};
});

it('returns the selected variable', () => {
const actual = fn(context, { name: 'test' }, handlers);
expect(actual).toEqual(1);
});

it('returns undefined if variable does not exist', () => {
const actual = fn(context, { name: 'unknown' }, handlers);
expect(actual).toEqual(undefined);
});
});
74 changes: 74 additions & 0 deletions src/plugins/expressions/public/functions/tests/var_set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { functionWrapper } from './utils';
import { variableSet } from '../var_set';
import { FunctionHandlers } from '../../../common/types';
import { KibanaContext } from '../../../common/expression_types/kibana_context';

describe('interpreter/functions#varset', () => {
const fn = functionWrapper(variableSet);
let context: Partial<KibanaContext>;
let initialContext: KibanaContext;
let handlers: FunctionHandlers;
let variables: Record<string, any>;

beforeEach(() => {
context = { timeRange: { from: '0', to: '1' } };
initialContext = {
type: 'kibana_context',
query: { language: 'lucene', query: 'geo.src:US' },
filters: [
{
meta: {
disabled: false,
negate: false,
alias: null,
},
query: { match: {} },
},
],
timeRange: { from: '2', to: '3' },
};
handlers = {
getInitialContext: () => initialContext,
variables: { test: 1 } as any,
};

variables = handlers.variables;
});

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

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

it('stores context if value is not set', () => {
const actual = fn(context, { name: 'test' }, handlers);
expect(variables.test).toEqual(context);
expect(actual).toEqual(context);
});
});
49 changes: 49 additions & 0 deletions src/plugins/expressions/public/functions/var.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunction } from '../../common/types';

interface Arguments {
name: string;
}

type Context = any;
type ExpressionFunctionVar = ExpressionFunction<'var', Context, Arguments, any>;

export const variable = (): ExpressionFunctionVar => ({
name: 'var',
help: i18n.translate('expressions.functions.var.help', {
defaultMessage: 'Updates kibana global context',
}),
args: {
name: {
types: ['string'],
aliases: ['_'],
required: true,
help: i18n.translate('expressions.functions.var.name.help', {
defaultMessage: 'Specify name of the variable',
}),
},
},
fn(context, args, handlers) {
const variables: Record<string, any> = handlers.variables;
return variables[args.name];
},
});
58 changes: 58 additions & 0 deletions src/plugins/expressions/public/functions/var_set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunction } from '../../common/types';

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

type Context = any;
type ExpressionFunctionVarSet = ExpressionFunction<'var_set', Context, Arguments, Context>;

export const variableSet = (): ExpressionFunctionVarSet => ({
name: 'var_set',
help: i18n.translate('expressions.functions.varset.help', {
defaultMessage: 'Updates kibana global context',
}),
args: {
name: {
types: ['string'],
aliases: ['_'],
required: true,
help: i18n.translate('expressions.functions.varset.name.help', {
defaultMessage: 'Specify name of the variable',
}),
},
value: {
aliases: ['val'],
help: i18n.translate('expressions.functions.varset.val.help', {
defaultMessage:
'Specify value for the variable. If not provided input context will be used',
}),
},
},
fn(context, args, handlers) {
const variables: Record<string, any> = handlers.variables;
variables[args.name] = args.value === undefined ? context : args.value;
return context;
},
});
3 changes: 3 additions & 0 deletions src/plugins/expressions/public/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export class ExpressionLoader {
if (params.extraHandlers && this.params) {
this.params.extraHandlers = params.extraHandlers;
}
if (params.variables && this.params) {
this.params.variables = params.variables;
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/expressions/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { clog as clogFunction } from './functions/clog';
import { font as fontFunction } from './functions/font';
import { kibana as kibanaFunction } from './functions/kibana';
import { kibanaContext as kibanaContextFunction } from './functions/kibana_context';
import { variable } from './functions/var';
import { variableSet } from './functions/var_set';
import {
boolean as booleanType,
datatable as datatableType,
Expand Down Expand Up @@ -109,6 +111,8 @@ export class ExpressionsPublicPlugin
registerFunction(fontFunction);
registerFunction(kibanaFunction);
registerFunction(kibanaContextFunction);
registerFunction(variable);
registerFunction(variableSet);

types.register(booleanType);
types.register(datatableType);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface IExpressionLoaderParams {
export interface IInterpreterHandlers {
getInitialContext: IGetInitialContext;
inspectorAdapters?: Adapters;
variables?: Record<string, any>;
abortSignal?: AbortSignal;
}

Expand Down