Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]
[dependencies]
napi = { version = "2.12.2", features = ["serde-json"] }
napi-derive = "2.12.2"
cel-interpreter = { path = "cel-rust/interpreter" }
cel-interpreter = { path = "cel-rust/interpreter", features = ["chrono"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Expand Down
8 changes: 8 additions & 0 deletions libs/core/__tests__/cel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { beforeEach, describe, expect, it } from "vitest";
import { CelProgram, evaluate } from "../src/index.js";

describe("evaluate", () => {
it("should add a duration to a timestamp using chrono feature", async () => {
const result = await evaluate(
"timestamp('2023-01-01T00:00:00Z') + duration('1h')",
{},
);
// The expected result is '2023-01-01T01:00:00Z' as an ISO string
expect(result).toBe("2023-01-01T01:00:00+00:00");
});
it("should evaluate a simple expression", async () => {
const result = await evaluate("size(message) > 5", {
message: "Hello World",
Expand Down
8 changes: 4 additions & 4 deletions libs/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "@kevinmichaelchen/cel-typescript-core",
"$schema": "node_modules/nx/schemas/project-schema.json",
"targets": {
"build": {
"executor": "nx:noop",
"dependsOn": ["build:native", "build:ts"]
},
"build:native": {
"cache": false,
"executor": "nx:run-commands",
Expand All @@ -26,10 +30,6 @@
},
"outputs": ["{projectRoot}/dist/**/*"]
},
"build": {
"executor": "nx:noop",
"dependsOn": ["build:native", "build:ts"]
},
"clean": {
"cache": false,
"executor": "nx:run-commands",
Expand Down
28 changes: 19 additions & 9 deletions libs/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(clippy::all)]

use cel_interpreter::{Context, Program, Value};
use napi_derive::napi;
use cel_interpreter::{Program, Context, Value};
use serde_json::Value as JsonValue;
use std::sync::Arc;

Expand All @@ -19,8 +19,8 @@ impl CelProgram {

#[napi]
pub fn compile(source: String) -> napi::Result<CelProgram> {
let program = Program::compile(&source)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
let program =
Program::compile(&source).map_err(|e| napi::Error::from_reason(e.to_string()))?;
Ok(CelProgram { program })
}

Expand All @@ -38,12 +38,12 @@ impl CelProgram {
} else {
Value::Null
}
},
}
JsonValue::String(s) => Value::String(Arc::new(s.clone())),
JsonValue::Array(arr) => {
let values: Vec<Value> = arr.iter().map(|v| Self::json_to_cel_value(v)).collect();
Value::List(Arc::new(values))
},
}
JsonValue::Object(map) => {
let mut cel_map = std::collections::HashMap::new();
for (key, value) in map {
Expand All @@ -65,27 +65,37 @@ impl CelProgram {
.unwrap_or(JsonValue::Null),
Value::String(s) => JsonValue::String((*s).to_string()),
Value::List(list) => JsonValue::Array(
list.iter().map(|v| Self::cel_to_json_value(v.clone())).collect()
list.iter()
.map(|v| Self::cel_to_json_value(v.clone()))
.collect(),
),
Value::Map(map) => JsonValue::Object(
map.map.iter().map(|(k, v)| (k.to_string(), Self::cel_to_json_value(v.clone()))).collect()
map.map
.iter()
.map(|(k, v)| (k.to_string(), Self::cel_to_json_value(v.clone())))
.collect(),
),
Value::Timestamp(ts) => JsonValue::String(ts.to_rfc3339()),
Value::Duration(dur) => {
JsonValue::Number(serde_json::Number::from(dur.num_nanoseconds().unwrap_or(0)))
}
_ => JsonValue::Null,
}
}

#[napi]
pub fn execute(&self, context: JsonValue) -> napi::Result<JsonValue> {
let mut ctx = Context::default();

if let JsonValue::Object(map) = context {
for (key, value) in map {
let cel_value = Self::json_to_cel_value(&value);
ctx.add_variable_from_value(key, cel_value);
}
}

self.program.execute(&ctx)
self.program
.execute(&ctx)
.map_err(|e| napi::Error::from_reason(e.to_string()))
.map(Self::cel_to_json_value)
}
Expand Down
Loading