@bufbuild/cel
Version:
A CEL evaluator for ECMAScript
139 lines (138 loc) • 3.85 kB
JavaScript
"use strict";
// Copyright 2024-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var _a, _b, _c;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMPTY_LIST = void 0;
exports.celList = celList;
exports.celListConcat = celListConcat;
exports.isCelList = isCelList;
const reflect_1 = require("@bufbuild/protobuf/reflect");
const proto_js_1 = require("./proto.js");
const value_js_1 = require("./value.js");
const privateSymbol = Symbol.for("@bufbuild/cel/list");
/**
* Create a new list from a native array or a ReflectList.
*/
function celList(arrayOrReflectList) {
if ((0, reflect_1.isReflectList)(arrayOrReflectList)) {
return new RepeatedFieldList(arrayOrReflectList);
}
return new ArrayList(arrayOrReflectList);
}
/**
* Returns a new List that has all the elements
* of the lists in order.
*/
function celListConcat(...lists) {
return new ConcatList(lists);
}
/**
* Returns true if the given value is a CelList.
*/
function isCelList(v) {
return typeof v === "object" && v !== null && privateSymbol in v;
}
class ArrayList {
constructor(_array) {
this._array = _array;
this[_a] = {};
}
get size() {
return this._array.length;
}
get(index) {
if (index < 0 || index >= this.size) {
return undefined;
}
return (0, value_js_1.toCel)(this._array[index]);
}
*values() {
for (const element of this._array.values()) {
yield (0, value_js_1.toCel)(element);
}
}
[(_a = privateSymbol, Symbol.iterator)]() {
return this.values();
}
}
class RepeatedFieldList {
constructor(_list) {
this._list = _list;
this[_b] = {};
}
get size() {
return this._list.size;
}
get(index) {
const val = this._list.get(index);
if (val === undefined) {
return undefined;
}
return celFromListElem(this._list.field(), val);
}
*values() {
for (const val of this._list) {
yield celFromListElem(this._list.field(), val);
}
}
[(_b = privateSymbol, Symbol.iterator)]() {
return this.values();
}
}
class ConcatList {
constructor(_lists) {
this._lists = _lists;
this[_c] = {};
let size = 0;
for (const list of _lists) {
size += list.size;
}
this._size = size;
}
get size() {
return this._size;
}
get(index) {
if (index < 0 || index >= this.size) {
return undefined;
}
for (const list of this._lists) {
if (index < list.size) {
return list.get(index);
}
index = index - list.size;
}
return undefined;
}
*values() {
for (const list of this._lists) {
yield* list.values();
}
}
[(_c = privateSymbol, Symbol.iterator)]() {
return this.values();
}
}
function celFromListElem(desc, v) {
switch (desc.listKind) {
case "enum":
return BigInt(v);
case "message":
return (0, value_js_1.reflectMsgToCel)(v);
case "scalar":
return (0, proto_js_1.celFromScalar)(desc.scalar, v);
}
}
exports.EMPTY_LIST = celList([]);