diff --git a/src/plugins/expressions/common/expression_types/filter.ts b/src/plugins/expressions/common/expression_types/filter.ts index 2608da6854b18..eb384fa0cd25b 100644 --- a/src/plugins/expressions/common/expression_types/filter.ts +++ b/src/plugins/expressions/common/expression_types/filter.ts @@ -25,6 +25,7 @@ const name = 'filter'; * Represents an object that is a Filter. */ export interface Filter { + version?: number; type?: string; value?: string; column?: string; @@ -34,9 +35,35 @@ export interface Filter { query?: string | null; } +const migrator = { + isUnsupported: (p: any) => !p.version || p.version < 2, + isLegacy: (p: any) => p.version && p.version < 3, + migrate: (legacyFilter: any) => ({ ...legacyFilter, version: 3 }), +}; + +function parseAndValidate(s: string): Filter { + try { + const parsed = JSON.parse(s); + if (migrator.isUnsupported(parsed)) { + throw new Error('LEGACY'); + } + return parsed; + } catch (e) { + if (e.message === 'LEGACY') { + throw new Error('Provided filter string is malformed or no longer supported'); + } + throw new Error('Unable to parse filter string.'); + } +} + export const filter = (): ExpressionType => ({ name, from: { + string: s => { + const { isLegacy, migrate } = migrator; + const parsed = parseAndValidate(s); + return isLegacy(parsed) ? migrate(parsed) : parsed; + }, null: () => { return { type: name, diff --git a/src/plugins/expressions/public/functions/combine_filters.ts b/src/plugins/expressions/public/functions/combine_filters.ts new file mode 100644 index 0000000000000..cae3058f6374a --- /dev/null +++ b/src/plugins/expressions/public/functions/combine_filters.ts @@ -0,0 +1,51 @@ +/* + * 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 { Filter } from '../../common/expression_types'; +import { ExpressionFunction } from '../../common/types'; + +interface Arguments { + filter: string; +} + +type Output = Filter; + +export function combineFilters(): ExpressionFunction<'combineFilters', null, Arguments, Output> { + return { + name: 'combineFilters', + type: 'filter', + help: 'takes two filters and merges them', + args: { + filter: { + types: ['filter'], + aliases: ['_'], + multi: true, + required: true, + help: 'filter object', + }, + }, + fn: (_context, args) => { + return { + type: 'filter', + ...Object.assign({}, ...args.filter), + and: [args.filter], // only here for illustrative purposes + }; + }, + }; +} diff --git a/src/plugins/expressions/public/plugin.ts b/src/plugins/expressions/public/plugin.ts index 034be58ec9e35..3c274ebebdef4 100644 --- a/src/plugins/expressions/public/plugin.ts +++ b/src/plugins/expressions/public/plugin.ts @@ -30,6 +30,7 @@ import { setNotifications, } from './services'; import { clog as clogFunction } from './functions/clog'; +import { combineFilters as combineFiltersFunction } from './functions/combine_filters'; import { font as fontFunction } from './functions/font'; import { kibana as kibanaFunction } from './functions/kibana'; import { kibanaContext as kibanaContextFunction } from './functions/kibana_context'; @@ -113,6 +114,7 @@ export class ExpressionsPublicPlugin }; registerFunction(clogFunction); + registerFunction(combineFiltersFunction); registerFunction(fontFunction); registerFunction(kibanaFunction); registerFunction(kibanaContextFunction);