Skip to content

Commit

Permalink
Merge in dep updates and bugfix (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMatt authored Nov 14, 2023
1 parent ee34093 commit 50aa6d9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ wasm = [


[dependencies]
test-case = "3.1.0"
regex = "1.9.4"
serde = { version = "1.0.188", features = ["derive", "rc"] }
serde_with = { version = "3.0.0", features = ["chrono"] }
serde_json = { version = "1.0.105", features = ["raw_value"] }
chrono = { version = "0.4.26", features = ["serde"] }
duration-str = "0.5.1"
test-case = "3.2.1"
regex = "1.10.2"
serde = { version = "1.0.190", features = ["derive", "rc"] }
serde_with = { version = "3.4.0", features = ["chrono"] }
serde_json = { version = "1.0.108", features = ["raw_value"] }
chrono = { version = "0.4.31", features = ["serde"] }
duration-str = "0.7.0"
num = "0.4.1"
cfg = "0.6.2"

# Dependencies for python bindings
pyo3 = { version = "0.19.2", optional = true, features = ["extension-module", "chrono"] }
pyo3 = { version = "0.20.0", optional = true, features = ["extension-module", "chrono"] }

# Dependencies for wasm bindings
wasm-bindgen = { version = "0.2.87", optional = true}
console_error_panic_hook = { version = "0.1.7", optional = true }
serde-wasm-bindgen = { version = "0.5.0", optional = true }
serde-wasm-bindgen = { version = "0.6.1", optional = true }
js-sys = { version = "0.3.64", optional = true }
20 changes: 10 additions & 10 deletions src/bindings/wasm/from_jsvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wasm_bindgen::{JsCast, JsValue};

use crate::{CelError, CelResult, CelValue};

use super::{object_iter::ObjectIterator, values};
use super::{log, object_iter::ObjectIterator, values};

fn extract_number_value<T: num::cast::FromPrimitive + FromStr>(
obj: &js_sys::Object,
Expand Down Expand Up @@ -50,7 +50,15 @@ fn extract_number_value<T: num::cast::FromPrimitive + FromStr>(
impl TryFrom<JsValue> for CelValue {
type Error = CelError;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
if value.is_object() {
if value.is_array() {
let mut list: Vec<CelValue> = Vec::new();

for list_value in values(&value).into_iter() {
list.push(list_value.try_into()?);
}

Ok(CelValue::from_list(list))
} else if value.is_object() {
let obj: js_sys::Object = value.into();

if obj.has_own_property(&"cel_float".into()) {
Expand Down Expand Up @@ -82,14 +90,6 @@ impl TryFrom<JsValue> for CelValue {
}
} else if value.is_string() {
Ok(CelValue::from_string(value.as_string().unwrap()))
} else if value.is_array() {
let mut list: Vec<CelValue> = Vec::new();

for list_value in values(&value).into_iter() {
list.push(list_value.try_into()?);
}

Ok(CelValue::from_list(list))
} else if value.is_null() || value.is_undefined() {
Ok(CelValue::from_null())
} else if value.is_truthy() {
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(msg: &str);
pub fn log(msg: &str);

#[wasm_bindgen(js_namespace = Object)]
fn keys(obj: &JsValue) -> js_sys::Array;
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ pub mod string_tokenizer;
pub mod syntax_error;
pub mod tokenizer;
pub mod tokens;

pub use compiler::CelCompiler;
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ mod test {
exec.bind_param("foo", 10.into());
assert_eq!(ctx.exec("entry", &exec).unwrap(), 13.into());
}

#[test]
fn test_object_access_in_array() {
let mut ctx = CelContext::new();
let mut exec = BindContext::new();

ctx.add_program_str("entry", "my_list[0].foo").unwrap();

let mut obj_map = HashMap::<String, CelValue>::new();
obj_map.insert("foo".to_owned(), "value".into());

let obj = CelValue::from_val_slice(&vec![obj_map.into()]);
exec.bind_param("my_list", obj);

assert_eq!(ctx.exec("entry", &exec).unwrap(), "value".into());
}
}

#[cfg(test)]
Expand Down

0 comments on commit 50aa6d9

Please sign in to comment.