@bufbuild/cel
Version:
A CEL evaluator for ECMAScript
81 lines (80 loc) • 2.38 kB
TypeScript
import { type CelType, type CelValueTuple, type CelInput } from "./type.js";
import { type CelResult } from "./error.js";
declare const privateFuncSymbol: unique symbol;
declare const privateOverloadSymbol: unique symbol;
export interface CallDispatch {
dispatch(id: number, args: CelResult[]): CelResult | undefined;
}
export interface Dispatcher {
find(name: string): CallDispatch | undefined;
}
/**
* A CEL function definition.
*/
export interface CelFunc extends CallDispatch {
[privateFuncSymbol]: unknown;
/**
* Name of the function.
*/
readonly name: string;
/**
* All the overloads of this function.
*/
readonly overloads: CelOverload<readonly CelType[], CelType>[];
}
/**
* Creates a new CelFunc.
*/
export declare function celFunc(name: string, overloads: CelOverload<readonly CelType[], CelType>[]): CelFunc;
/**
* A CEL function overload.
*/
export interface CelOverload<P extends readonly CelType[], R extends CelType> {
[privateOverloadSymbol]: unknown;
/**
* Array of parameter types.
*/
readonly parameters: P;
/**
* The result type.
*/
readonly result: R;
/**
* Implementation for this overload.
*/
readonly impl: (...args: CelValueTuple<P>) => CelInput<R>;
}
/**
* Creates a new CelOverload.
*/
export declare function celOverload<const P extends readonly CelType[], const R extends CelType>(parameters: P, result: R, impl: (...args: CelValueTuple<P>) => CelInput<R>): CelOverload<P, R>;
/**
* Set of functions uniquely identified by their name.
*/
export declare class FuncRegistry implements Dispatcher {
private functions;
constructor(funcs?: CelFunc[]);
/**
* Adds a new function to the registry.
*
* Throws an error if the function with the same name is already added.
*/
add(func: CelFunc): void;
add(funcs: CelFunc[]): void;
/**
* Adds a function by name and the call.
*/
add(name: string, call: CallDispatch): void;
/**
* Find a function by name.
*/
find(name: string): CallDispatch | undefined;
private addCall;
}
export declare class OrderedDispatcher implements Dispatcher {
private readonly dispatchers;
constructor(dispatchers: Dispatcher[]);
add(dispatcher: Dispatcher): void;
find(name: string): CallDispatch | undefined;
}
export {};