Clean-up expression "run" methods (#70795)
* chore: 🤖 remove legacy "interpret" methods in expressions * fix: 🐛 correctly use the moved "interpret" method * feat: 🎸 pass through execution context to sub-expressions * chore: 🤖 remove not used imports
This commit is contained in:
parent
2399780d99
commit
f28d4e920e
2 changed files with 28 additions and 32 deletions
|
@ -18,7 +18,7 @@
|
|||
*/
|
||||
|
||||
import { keys, last, mapValues, reduce, zipObject } from 'lodash';
|
||||
import { Executor } from '../executor';
|
||||
import { Executor, ExpressionExecOptions } from '../executor';
|
||||
import { createExecutionContainer, ExecutionContainer } from './container';
|
||||
import { createError } from '../util';
|
||||
import { Defer, now } from '../../../kibana_utils/common';
|
||||
|
@ -31,6 +31,7 @@ import {
|
|||
parse,
|
||||
formatExpression,
|
||||
parseExpression,
|
||||
ExpressionAstNode,
|
||||
} from '../ast';
|
||||
import { ExecutionContext, DefaultInspectorAdapters } from './types';
|
||||
import { getType, ExpressionValue } from '../expression_types';
|
||||
|
@ -382,7 +383,7 @@ export class Execution<
|
|||
const resolveArgFns = mapValues(argAstsWithDefaults, (asts, argName) => {
|
||||
return asts.map((item: ExpressionAstExpression) => {
|
||||
return async (subInput = input) => {
|
||||
const output = await this.params.executor.interpret(item, subInput, {
|
||||
const output = await this.interpret(item, subInput, {
|
||||
debug: this.params.debug,
|
||||
});
|
||||
if (isExpressionValueError(output)) throw output.error;
|
||||
|
@ -415,4 +416,28 @@ export class Execution<
|
|||
// function which would be treated as a promise
|
||||
return { resolvedArgs };
|
||||
}
|
||||
|
||||
public async interpret<T>(
|
||||
ast: ExpressionAstNode,
|
||||
input: T,
|
||||
options?: ExpressionExecOptions
|
||||
): Promise<unknown> {
|
||||
switch (getType(ast)) {
|
||||
case 'expression':
|
||||
const execution = this.params.executor.createExecution(
|
||||
ast as ExpressionAstExpression,
|
||||
this.context,
|
||||
options
|
||||
);
|
||||
execution.start(input);
|
||||
return await execution.result;
|
||||
case 'string':
|
||||
case 'number':
|
||||
case 'null':
|
||||
case 'boolean':
|
||||
return ast;
|
||||
default:
|
||||
throw new Error(`Unknown AST object: ${JSON.stringify(ast)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,8 +26,7 @@ import { Execution, ExecutionParams } from '../execution/execution';
|
|||
import { IRegistry } from '../types';
|
||||
import { ExpressionType } from '../expression_types/expression_type';
|
||||
import { AnyExpressionTypeDefinition } from '../expression_types/types';
|
||||
import { getType } from '../expression_types';
|
||||
import { ExpressionAstExpression, ExpressionAstNode } from '../ast';
|
||||
import { ExpressionAstExpression } from '../ast';
|
||||
import { typeSpecs } from '../expression_types/specs';
|
||||
import { functionSpecs } from '../expression_functions/specs';
|
||||
|
||||
|
@ -154,34 +153,6 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u
|
|||
return this.state.selectors.getContext();
|
||||
}
|
||||
|
||||
public async interpret<T>(
|
||||
ast: ExpressionAstNode,
|
||||
input: T,
|
||||
options?: ExpressionExecOptions
|
||||
): Promise<unknown> {
|
||||
switch (getType(ast)) {
|
||||
case 'expression':
|
||||
return await this.interpretExpression(ast as ExpressionAstExpression, input, options);
|
||||
case 'string':
|
||||
case 'number':
|
||||
case 'null':
|
||||
case 'boolean':
|
||||
return ast;
|
||||
default:
|
||||
throw new Error(`Unknown AST object: ${JSON.stringify(ast)}`);
|
||||
}
|
||||
}
|
||||
|
||||
public async interpretExpression<T>(
|
||||
ast: string | ExpressionAstExpression,
|
||||
input: T,
|
||||
options?: ExpressionExecOptions
|
||||
): Promise<unknown> {
|
||||
const execution = this.createExecution(ast, undefined, options);
|
||||
execution.start(input);
|
||||
return await execution.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute expression and return result.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue