Skip to content

Commit

Permalink
Refactor filters unboxing
Browse files Browse the repository at this point in the history
  • Loading branch information
dokmic committed Mar 22, 2021
1 parent 7ec0933 commit 7bbbe60
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/plugins/data/common/search/expressions/kibana_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import { uniqBy } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition, ExecutionContext } from 'src/plugins/expressions/common';
import {
ExpressionFunctionDefinition,
ExecutionContext,
unboxExpressionValue,
} from 'src/plugins/expressions/common';
import { Adapters } from 'src/plugins/inspector/common';
import { Query, uniqFilters } from '../../query';
import { ExecutionContextSearch, KibanaContext, KibanaFilter } from './kibana_context_type';
Expand Down Expand Up @@ -81,14 +85,7 @@ export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
async fn(input, args, { getSavedObject }) {
const timeRange = args.timeRange || input?.timeRange;
let queries = mergeQueries(input?.query, args?.q || []);
let filters = [
...(input?.filters || []),
...(args?.filters?.map((f) => {
// @ts-ignore
delete f.type;
return f;
}) || []),
];
let filters = [...(input?.filters || []), ...(args?.filters?.map(unboxExpressionValue) || [])];

if (args.savedSearchId) {
if (typeof getSavedObject !== 'function') {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/common/expression_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './get_type';
export * from './serialize_provider';
export * from './expression_type';
export * from './specs';
export * from './unbox_expression_value';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { unboxExpressionValue } from './unbox_expression_value';

describe('unboxExpressionValue()', () => {
it('should remove type property from a boxed value', () => {
const expressionValue = { type: 'something', value: 'something' };

expect(unboxExpressionValue(expressionValue)).toEqual({ value: 'something' });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ExpressionValueBoxed } from './types';

export function unboxExpressionValue<T extends object>({
type,
...value
}: ExpressionValueBoxed<string, T>): T {
return value as T;
}

0 comments on commit 7bbbe60

Please sign in to comment.