Skip to content

Commit

Permalink
Add pared feature which integrates the pared dependency so that you can
Browse files Browse the repository at this point in the history
Use Arc projection refs to documents avoiding heavy clones when using a dynamic resolver.

Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino committed Jul 6, 2024
1 parent 3d8f701 commit d8fa696
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
4 changes: 4 additions & 0 deletions interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ edition = "2018"
license = "MIT"
categories = ["interpreter", "cel"]

[features]
pared = ["dep:pared"]

[dependencies]
cel-parser = { path = "../parser", version = "0.6.0" }
thiserror = "1.0.40"
Expand All @@ -16,6 +19,7 @@ nom = "7.1.3"
paste = "1.0.14"
serde = "1.0.196"
regex = "1.10.5"
pared = { version="0.3.0", optional = true }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
34 changes: 18 additions & 16 deletions interpreter/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ mod tests {
use std::sync::Arc;

use crate::context::Context;
use crate::objects::{DynamicCollection, MemberResolver, ResolvedMember};
use crate::testing::test_script;
use crate::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -789,18 +788,21 @@ mod tests {
assert_eq!(test_script("hello == 'world'", Some(ctx)), Ok(true.into()));
}

#[cfg(feature = "pared")]
#[test]
fn test_deep_dynamic_resolver() {
use crate::objects::{MemberResolver, ParcDynamicCollection, ResolvedMember};
use pared::sync::Parc;

#[derive(Clone)]
struct Species {
name: String,
language: Option<String>,
homeworld: Option<String>,
}

impl DynamicCollection for Species {
fn resolver(&self) -> MemberResolver {
let receiver = self.clone();
impl ParcDynamicCollection<Species> for Species {
fn resolver(receiver: Parc<Species>) -> MemberResolver {
MemberResolver::new(move |name| match name {
ResolvedMember::Attribute(name) => match name.as_str() {
"name" => Some(receiver.name.clone().into()),
Expand All @@ -826,17 +828,16 @@ mod tests {
species: Species,
}

impl DynamicCollection for Character {
fn resolver(&self) -> MemberResolver {
let receiver = self.clone();
impl ParcDynamicCollection<Character> for Character {
fn resolver(receiver: Parc<Character>) -> MemberResolver {
MemberResolver::new(move |member| match member {
ResolvedMember::Attribute(name) => match name.as_str() {
"name" => Some(receiver.name.clone().into()),
"gender" => match receiver.gender.clone() {
Some(v) => Some(v.clone().into()),
None => Some(Value::Null),
},
"species" => Some(receiver.species.clone().into()),
"species" => Some(receiver.project(|receiver| &receiver.species).into()),
_ => None,
},
_ => None,
Expand All @@ -851,9 +852,8 @@ mod tests {
characters: Vec<Character>,
}

impl DynamicCollection for Film {
fn resolver(&self) -> MemberResolver {
let receiver = self.clone();
impl ParcDynamicCollection<Film> for Film {
fn resolver(receiver: Parc<Film>) -> MemberResolver {
MemberResolver::new(move |member| {
let receiver = receiver.clone();
match member {
Expand All @@ -865,9 +865,11 @@ mod tests {
let receiver = receiver.clone();
match member {
ResolvedMember::Index(Value::Int(idx)) => receiver
.characters
.get(idx as usize)
.map(|v| v.clone().into()),
.try_project(|receiver| {
receiver.characters.get(idx as usize).ok_or(())
})
.ok()
.map(|x| x.into()),
_ => None,
}
},
Expand All @@ -880,7 +882,7 @@ mod tests {
}
}

let doc = Film {
let doc = Parc::new(Film {
director: "George Lucas".to_string(),
title: "A New Hope".to_string(),
characters: vec![
Expand Down Expand Up @@ -912,7 +914,7 @@ mod tests {
},
},
],
};
});

let mut ctx = Context::default();
ctx.set_dynamic_resolver(move |name| match name {
Expand Down
27 changes: 23 additions & 4 deletions interpreter/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,20 +707,39 @@ pub trait DynamicCollection {
fn resolver(&self) -> MemberResolver;
}

impl <T> From<T> for Value
where T: DynamicCollection {
impl<T> From<T> for Value
where
T: DynamicCollection,
{
fn from(item: T) -> Self {
Value::DynamicCollection(item.resolver())
}
}

impl <T> From<Arc<T>> for Value
where T: DynamicCollection {
impl<T> From<Arc<T>> for Value
where
T: DynamicCollection,
{
fn from(item: Arc<T>) -> Self {
Value::DynamicCollection(item.resolver())
}
}

#[cfg(feature = "pared")]
pub trait ParcDynamicCollection<T> {
fn resolver(item: pared::sync::Parc<T>) -> MemberResolver;
}

#[cfg(feature = "pared")]
impl<T> From<pared::sync::Parc<T>> for Value
where
T: ParcDynamicCollection<T>,
{
fn from(item: pared::sync::Parc<T>) -> Self {
Value::DynamicCollection(T::resolver(item))
}
}

impl ops::Add<Value> for Value {
type Output = Value;

Expand Down

0 comments on commit d8fa696

Please sign in to comment.