Skip to content

Commit

Permalink
fixing getByAlias
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Mar 10, 2022
1 parent a35b17c commit 0041e3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/plugins/expressions/common/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ExpressionType } from '../expression_types/expression_type';
import { AnyExpressionTypeDefinition } from '../expression_types/types';
import { ExpressionAstExpression, ExpressionAstFunction } from '../ast';
import { ExpressionValueError, typeSpecs } from '../expression_types/specs';
import { getByAlias } from '../util';
import { ALL_NAMESPACES, getByAlias } from '../util';
import { SavedObjectReference } from '../../../../core/types';
import {
MigrateFunctionsObject,
Expand Down Expand Up @@ -227,7 +227,7 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u
const functions = this.container.get().functions;
for (const link of ast.chain) {
const { function: fnName, arguments: fnArgs } = link;
const fn = getByAlias(functions, fnName);
const fn = getByAlias(functions, fnName, ALL_NAMESPACES);

if (fn) {
// if any of arguments are expressions we should migrate those first
Expand Down Expand Up @@ -258,7 +258,7 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u
(newAst: ExpressionAstExpression, funcAst: ExpressionAstFunction, index: number) => {
const realIndex = index + additionalFunctions;
const { function: fnName, arguments: fnArgs } = funcAst;
const fn = getByAlias(functions, fnName);
const fn = getByAlias(functions, fnName, ALL_NAMESPACES);
if (!fn) {
return newAst;
}
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/expressions/common/util/get_by_alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* the given object/array for a case-insensitive match, which could be either the
* `name` itself, or something under the `aliases` property.
*/

export const ALL_NAMESPACES = '*';

export function getByAlias<T extends { name?: string; aliases?: string[]; namespace?: string }>(
node: T[] | Record<string, T>,
nodeName: string,
Expand All @@ -20,10 +23,13 @@ export function getByAlias<T extends { name?: string; aliases?: string[]; namesp
return Object.values(node).find(({ name, aliases, namespace }) => {
if (!name) return false;
if (name.toLowerCase() === lowerCaseName) {
if (!namespace || namespace === nameSpace) return true;
if (!namespace || nameSpace === ALL_NAMESPACES || namespace === nameSpace) return true;
}
return (aliases || []).some((alias) => {
return alias.toLowerCase() === lowerCaseName && (!namespace || namespace === nameSpace);
return (
alias.toLowerCase() === lowerCaseName &&
(!namespace || nameSpace === ALL_NAMESPACES || namespace === nameSpace)
);
});
});
}

0 comments on commit 0041e3b

Please sign in to comment.