-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[skip-ci] POC: Using expression types to "migrate" state passed in args. #57354
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<typeof name, Filter> => ({ | ||
name, | ||
from: { | ||
string: s => { | ||
const { isLegacy, migrate } = migrator; | ||
const parsed = parseAndValidate(s); | ||
return isLegacy(parsed) ? migrate(parsed) : parsed; | ||
}, | ||
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole point here is that the "owner" of filters can set up the casting/migrations without the author of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also worth pointing out that @streamich has floated the idea of making type converters pluggable by third parties in some future version of the expressions service: registerTypeConverter('string', 'filter', (string) => { /*...*/ }) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be built on top of the general migration system, but my point was, who is writing I think it would work if it used the generic persistableStateFactory we talked about. Code could look something like
I was thinking |
||
null: () => { | ||
return { | ||
type: name, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key here is that we still specify The If for some reason this function needed a legacy filter shape, either multiple types could be registered as a means of tracking version ( |
||
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 | ||
}; | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some throwaway code to run a fake migration which simply increments the version number. If version 1 is provided, we consider this to be no longer supported and throw an error. If version 2 is provided, we provide the ability to migrate it up to the latest (version 3).
How these migrations are structured and standardized is a separate topic; this is only an example.