Skip to content

Commit

Permalink
feat: In expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Sep 2, 2024
1 parent 5115270 commit a25fb88
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lykiadb-server/src/value/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ impl RV {
}
self.as_bool().partial_cmp(&other)
}

pub fn is_in(&self, other: &RV) -> RV {
match (self, other) {
(RV::Str(lhs), RV::Str(rhs)) => {
RV::Bool(rhs.contains(lhs.as_str()))
}
(lhs, RV::Array(rhs)) => {
RV::Bool(rhs.read().unwrap().contains(&lhs))
}
(RV::Str(key), RV::Object(map)) => {
RV::Bool(map.read().unwrap().contains_key(key.as_str()))
},
_ => RV::Bool(false),
}
}

pub fn not(&self) -> RV {
RV::Bool(!self.as_bool())
}
}

impl PartialEq for RV {
Expand Down Expand Up @@ -375,12 +394,12 @@ pub fn eval_binary(left_eval: RV, right_eval: RV, operation: Operation) -> RV {
Operation::Subtract => left_eval - right_eval,
Operation::Multiply => left_eval * right_eval,
Operation::Divide => left_eval / right_eval,
Operation::In => left_eval.is_in(&right_eval),
Operation::NotIn => left_eval.is_in(&right_eval).not(),
// TODO: Implement operations:
/*
Operation::Like
Operation::NotLike
Operation::In
Operation::NotIn
*/
_ => RV::Undefined,
}
Expand Down

0 comments on commit a25fb88

Please sign in to comment.