@bufbuild/cel
Version:
A CEL evaluator for ECMAScript
180 lines (179 loc) • 6.68 kB
TypeScript
import type { DescMessage, Message, MessageShape } from "@bufbuild/protobuf";
import { type CelList } from "./list.js";
import { type CelMap } from "./map.js";
import { type CelUint } from "./uint.js";
import { type ReflectList, type ReflectMap, type ReflectMessage } from "@bufbuild/protobuf/reflect";
declare const privateSymbol: unique symbol;
/**
* Types of CEL values.
*
* Ref: https://github.com/google/cel-spec/blob/master/doc/langdef.md#values
*/
export type CelType = CelListType | CelMapType | CelObjectType | CelTypeType | CelScalarType;
/**
* Scalar CEL value types.
*/
export declare const CelScalar: {
readonly INT: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "int";
readonly name: "int";
readonly toString: () => "int";
};
readonly UINT: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "uint";
readonly name: "uint";
readonly toString: () => "uint";
};
readonly BOOL: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "bool";
readonly name: "bool";
readonly toString: () => "bool";
};
readonly STRING: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "string";
readonly name: "string";
readonly toString: () => "string";
};
readonly BYTES: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "bytes";
readonly name: "bytes";
readonly toString: () => "bytes";
};
readonly DOUBLE: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "double";
readonly name: "double";
readonly toString: () => "double";
};
readonly NULL: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "null_type";
readonly name: "null_type";
readonly toString: () => "null_type";
};
readonly DYN: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "dyn";
readonly name: "dyn";
readonly toString: () => "dyn";
};
readonly TYPE: {
readonly [privateSymbol]: {};
readonly kind: "scalar";
readonly scalar: "type";
readonly name: "type";
readonly toString: () => "type";
};
};
/**
* Represents a CEL scalar type.
*/
export type CelScalarType = (typeof CelScalar)[keyof typeof CelScalar];
/**
* Represents a CEL list type.
*/
export interface CelListType<E extends CelType = CelType> extends celTypeShared {
readonly kind: "list";
readonly element: E;
readonly name: "list";
}
/**
* Represents a CEL map type.
*/
export interface CelMapType<K extends mapKeyType = mapKeyType, V extends CelType = CelType> extends celTypeShared {
readonly kind: "map";
readonly key: K;
readonly value: V;
readonly name: "map";
}
/**
* Represents a CEL type.
*/
export interface CelTypeType<T extends CelType = CelType> extends celTypeShared {
readonly kind: "type";
readonly type: T;
readonly name: "type";
}
export interface CelObjectType<Desc extends DescMessage = DescMessage> extends celTypeShared {
readonly kind: "object";
readonly desc: Desc;
readonly name: Desc["typeName"];
}
interface celTypeShared {
[privateSymbol]: unknown;
/**
* Runtime type name of the type. Used for equality check.
*/
readonly name: string;
toString(): string;
}
/**
* Represents the CEL type google.protobuf.Timestamp.
*/
export declare const TIMESTAMP: CelObjectType<import("@bufbuild/protobuf/codegenv2").GenMessage<import("@bufbuild/protobuf/wkt").Timestamp, {
jsonType: import("@bufbuild/protobuf/wkt").TimestampJson;
}>>;
/**
* Represents the CEL type google.protobuf.Duration.
*/
export declare const DURATION: CelObjectType<import("@bufbuild/protobuf/codegenv2").GenMessage<import("@bufbuild/protobuf/wkt").Duration, {
jsonType: import("@bufbuild/protobuf/wkt").DurationJson;
}>>;
/**
* Creates a new CelMapType.
*/
export declare function mapType<const K extends CelMapType["key"], const V extends CelType>(key: K, value: V): CelMapType<K, V>;
/**
* Creates a new CelListType.
*/
export declare function listType<const E extends CelListType["element"]>(element: E): CelListType<E>;
/**
* Creates a new CelTypeType
*/
export declare function typeType<const T extends CelType>(type: T): CelTypeType<T>;
/**
* Creates a new CelObjectType.
*/
export declare function objectType<const Desc extends DescMessage>(desc: Desc): CelObjectType<Desc>;
type mapKeyType = typeof CelScalar.INT | typeof CelScalar.UINT | typeof CelScalar.BOOL | typeof CelScalar.STRING | typeof CelScalar.DYN;
/**
* CEL values corresponding to their type.
*/
export type CelValue<T extends CelType = CelType> = T extends typeof CelScalar.DYN ? celValue : celValue<T>;
type celValue<T extends CelType = CelType> = T extends typeof CelScalar.TYPE ? CelType : T extends typeof CelScalar.INT ? bigint : T extends typeof CelScalar.UINT ? CelUint : T extends typeof CelScalar.DOUBLE ? number : T extends typeof CelScalar.BOOL ? boolean : T extends typeof CelScalar.STRING ? string : T extends typeof CelScalar.BYTES ? Uint8Array : T extends typeof CelScalar.NULL ? null : T extends CelListType ? CelList : T extends CelMapType ? CelMap : T extends CelTypeType ? CelType : T extends CelObjectType<infer Desc> ? Message extends MessageShape<Desc> ? ReflectMessage : ReflectMessage & {
message: MessageShape<Desc>;
} : never;
/**
* Values that are accepted as CEL values.
*/
export type CelInput<T extends CelType = CelType> = T extends typeof CelScalar.DYN ? celInput : celInput<T>;
type celInput<T extends CelType = CelType> = T extends CelListType ? CelList | readonly celInput[] | ReflectList : T extends CelMapType ? CelMap | ReadonlyMap<celInput<mapKeyType>, celInput> | ReflectMap | {
[key: string]: celInput;
} : T extends CelObjectType ? ReflectMessage | Message : celValue<T>;
export type CelValueTuple<T extends readonly CelType[]> = T extends readonly [
infer First extends CelType,
...infer Rest extends CelType[]
] ? [CelValue<First>, ...CelValueTuple<Rest>] : CelType[] extends T ? any[] : [];
/**
* Get the CelType of a CelValue.
*/
export declare function celType(v: CelValue): CelType;
/**
* Returns true if the given value is a CelType.
*/
export declare function isCelType(v: unknown): v is CelType;
export declare function isObjectCelType(v: NonNullable<object>): v is CelType;
export {};