-
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
Expressions/migrations2 #81281
Expressions/migrations2 #81281
Changes from 3 commits
21e2501
d73f31d
15d9305
f122bdf
31513e7
3afcbe8
134e82c
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [Executor](./kibana-plugin-plugins-expressions-public.executor.md) > [migrate](./kibana-plugin-plugins-expressions-public.executor.migrate.md) | ||
|
||
## Executor.migrate() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrate(ast: SerializableState, version: string): ExpressionAstExpression; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| ast | <code>SerializableState</code> | | | ||
| version | <code>string</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`ExpressionAstExpression` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [Executor](./kibana-plugin-plugins-expressions-public.executor.md) > [migrateToLatest](./kibana-plugin-plugins-expressions-public.executor.migratetolatest.md) | ||
|
||
## Executor.migrateToLatest() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrateToLatest(ast: unknown, version: string): ExpressionAstExpression; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| ast | <code>unknown</code> | | | ||
| version | <code>string</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`ExpressionAstExpression` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [ExpressionFunction](./kibana-plugin-plugins-expressions-public.expressionfunction.md) > [migrations](./kibana-plugin-plugins-expressions-public.expressionfunction.migrations.md) | ||
|
||
## ExpressionFunction.migrations property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrations: { | ||
[key: string]: (state: SerializableState) => SerializableState; | ||
}; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [ExpressionsService](./kibana-plugin-plugins-expressions-public.expressionsservice.md) > [migrate](./kibana-plugin-plugins-expressions-public.expressionsservice.migrate.md) | ||
|
||
## ExpressionsService.migrate property | ||
|
||
Injects saved object references into expression AST | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
readonly migrate: (state: SerializableState, version: string) => ExpressionAstExpression; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [ExpressionsService](./kibana-plugin-plugins-expressions-public.expressionsservice.md) > [migrateToLatest](./kibana-plugin-plugins-expressions-public.expressionsservice.migratetolatest.md) | ||
|
||
## ExpressionsService.migrateToLatest property | ||
|
||
Injects saved object references into expression AST | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
readonly migrateToLatest: (state: unknown, version: string) => ExpressionAstExpression; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-server](./kibana-plugin-plugins-expressions-server.md) > [Executor](./kibana-plugin-plugins-expressions-server.executor.md) > [migrate](./kibana-plugin-plugins-expressions-server.executor.migrate.md) | ||
|
||
## Executor.migrate() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrate(ast: SerializableState, version: string): ExpressionAstExpression; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| ast | <code>SerializableState</code> | | | ||
| version | <code>string</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`ExpressionAstExpression` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-server](./kibana-plugin-plugins-expressions-server.md) > [Executor](./kibana-plugin-plugins-expressions-server.executor.md) > [migrateToLatest](./kibana-plugin-plugins-expressions-server.executor.migratetolatest.md) | ||
|
||
## Executor.migrateToLatest() method | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrateToLatest(ast: unknown, version: string): ExpressionAstExpression; | ||
``` | ||
|
||
## Parameters | ||
|
||
| Parameter | Type | Description | | ||
| --- | --- | --- | | ||
| ast | <code>unknown</code> | | | ||
| version | <code>string</code> | | | ||
|
||
<b>Returns:</b> | ||
|
||
`ExpressionAstExpression` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-expressions-server](./kibana-plugin-plugins-expressions-server.md) > [ExpressionFunction](./kibana-plugin-plugins-expressions-server.expressionfunction.md) > [migrations](./kibana-plugin-plugins-expressions-server.expressionfunction.migrations.md) | ||
|
||
## ExpressionFunction.migrations property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
migrations: { | ||
[key: string]: (state: SerializableState) => SerializableState; | ||
}; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ import { typeSpecs } from '../expression_types/specs'; | |
import { functionSpecs } from '../expression_functions/specs'; | ||
import { getByAlias } from '../util'; | ||
import { SavedObjectReference } from '../../../../core/types'; | ||
import { PersistableState } from '../../../kibana_utils/common'; | ||
import { PersistableState, SerializableState } from '../../../kibana_utils/common'; | ||
|
||
export interface ExpressionExecOptions { | ||
/** | ||
|
@@ -257,6 +257,21 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u | |
return telemetryData; | ||
} | ||
|
||
migrate(ast: SerializableState, version: string) { | ||
return this.walkAst(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => { | ||
link = fn.migrations[version](link) as ExpressionAstFunction; | ||
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. and we expect the migration to return ExpressionAstFunction. thats not the general case with migrations, we might be working with unknown states where the only thing we know is that its serializable. When we do have more knowledge we'll need to do the casting. ps: open for suggestions on how to better type safe this 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. Yeah the only other option that comes to mind is instead casting |
||
}); | ||
} | ||
|
||
migrateToLatest(ast: unknown, version: string) { | ||
return this.walkAst(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => { | ||
for (const key in Object.keys(fn.migrations)) { | ||
if (key < version) continue; | ||
link = fn.migrations[key](link) as ExpressionAstFunction; | ||
} | ||
}); | ||
} | ||
|
||
public fork(): Executor<Context> { | ||
const initialState = this.state.get(); | ||
const fork = new Executor<Context>(initialState); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from '../types'; | ||
import { ExpressionValueSearchContext } from '../../expression_types'; | ||
import { MigrateFunction } from '../../../../kibana_utils/common/persistable_state'; | ||
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. all changes to this file will be reverted, its just an example |
||
|
||
const toArray = <T>(query: undefined | T | T[]): T[] => | ||
!query ? [] : Array.isArray(query) ? query : [query]; | ||
|
@@ -32,6 +33,23 @@ export type ExpressionFunctionKibana = ExpressionFunctionDefinition< | |
ExpressionValueSearchContext | ||
>; | ||
|
||
type MYSTATE_7_10_0 = { | ||
name: 'kibana'; | ||
args: {}; | ||
}; | ||
|
||
type MYSTATE_7_10_1 = { | ||
name: 'kibana_input'; | ||
args: {}; | ||
}; | ||
|
||
const MIGRATE7_10_1: MigrateFunction<MYSTATE_7_10_0, MYSTATE_7_10_1> = (state) => { | ||
return { | ||
name: 'kibana_input', | ||
args: state.args, | ||
}; | ||
}; | ||
|
||
export const kibana: ExpressionFunctionKibana = { | ||
name: 'kibana', | ||
type: 'kibana_context', | ||
|
@@ -44,6 +62,10 @@ export const kibana: ExpressionFunctionKibana = { | |
|
||
args: {}, | ||
|
||
migrations: { | ||
'7.10.1': (MIGRATE7_10_1 as any) as MigrateFunction, | ||
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. this is the part i really don't like. any suggestions ? 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. Why doesn't TS like this? Looks as if it should work without casting to me, but need to test it 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. Maybe part of the issue is that in 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 problem is that MigrateFunction there is <SerializableSTate,SerializableState> where MIGRATE7_10_1 actually work with well defined types 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.
Okay, after looking into this more, there are actually a couple problems. First, More problematic is that The bummer is that this makes things more verbose and makes interface Migrations extends Record<string, any> {
'7.10.1': MigrateFunction<MYSTATE_7_10_0, MYSTATE_7_10_1>;
}
export type ExpressionFunctionKibana = ExpressionFunctionDefinition<
'kibana',
ExpressionValueSearchContext | null,
object,
ExpressionValueSearchContext,
ExecutionContext,
Migrations
>; And then Migrations extends Record<string, MigrateFunction> = Record<string, MigrateFunction> I tested this and it worked for me locally, but LMK what you think. Either way if we are adding |
||
}, | ||
|
||
fn(input, _, { search = {} }) { | ||
const output: ExpressionValueSearchContext = { | ||
// TODO: This spread is left here for legacy reasons, possibly Lens uses it. | ||
|
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.
we know the input is ExpressionAstExpression as we don't allow migrating that structure but just the actual functions within