Skip to content

Commit be01ef4

Browse files
authored
feat: __add__ and __eq__ functionality in python (#68)
* Working rust impl * Working Python impl * Add eq functionality * Fmt * Add types for __add__
1 parent a3408d6 commit be01ef4

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

cql2.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ class Expr:
118118
['LC82030282019133LGN00']
119119
"""
120120

121+
def __add__(self, other: "Expr") -> "Expr":
122+
"""Combines two cql2 expressions using the AND operator.
123+
124+
Args:
125+
other (Expr): The other expression
126+
127+
Returns:
128+
Expr: The combined expression
129+
130+
Examples:
131+
>>> from cql2 import Expr
132+
>>> expr1 = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
133+
>>> expr2 = Expr("landsat:cloud_cover = 10")
134+
>>> expr = expr1 + expr2
135+
"""
136+
121137
class SqlQuery:
122138
"""A SQL query"""
123139

python/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ impl Expr {
8585
fn to_sql(&self) -> Result<SqlQuery> {
8686
self.0.to_sql().map(SqlQuery::from).map_err(Error::from)
8787
}
88+
89+
fn __add__(&self, rhs: &Expr) -> Result<Expr> {
90+
Ok(Expr(self.0.clone() + rhs.0.clone()))
91+
}
92+
93+
fn __eq__(&self, rhs: &Expr) -> bool {
94+
self.0 == rhs.0
95+
}
8896
}
8997

9098
impl From<::cql2::SqlQuery> for SqlQuery {

python/tests/test_expr.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,11 @@ def test_validate() -> None:
5757
)
5858
with pytest.raises(ValidationError):
5959
expr.validate()
60+
61+
62+
def test_add() -> None:
63+
assert Expr("True") + Expr("false") == Expr("true AND false")
64+
65+
66+
def test_eq() -> None:
67+
assert Expr("True") == Expr("true")

src/expr.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use pg_escape::{quote_identifier, quote_literal};
77
use serde::{Deserialize, Serialize};
88
use serde_json::Value;
99
use std::collections::HashSet;
10-
use std::ops::Deref;
10+
use std::ops::{Add, Deref};
1111
use std::str::FromStr;
1212
use unaccent::unaccent;
1313
use wkt::TryFromWkt;
@@ -614,6 +614,41 @@ impl FromStr for Expr {
614614
}
615615
}
616616
}
617+
618+
impl Add for Expr {
619+
type Output = Expr;
620+
621+
///
622+
/// Combines two expressions with the `+` operator.
623+
///
624+
/// # Examples
625+
///
626+
/// ```
627+
/// use cql2::Expr;
628+
/// use std::ops::Add;
629+
///
630+
/// let expr1 = Expr::Bool(true);
631+
/// let expr2 = Expr::Bool(false);
632+
/// let expected_expr: Expr = "true and false".parse().unwrap();
633+
/// assert_eq!(expr1 + expr2, expected_expr);
634+
/// ```
635+
///
636+
/// ```
637+
/// use cql2::Expr;
638+
/// use std::ops::Add;
639+
///
640+
/// let expr1 = Expr::Bool(true);
641+
/// let expr2 = Expr::Bool(false);
642+
/// let expected_expr: Expr = "true and false".parse().unwrap();
643+
/// assert_eq!(expr1.add(expr2), expected_expr);
644+
/// ```
645+
fn add(self, other: Expr) -> Expr {
646+
Expr::Operation {
647+
op: "and".to_string(),
648+
args: vec![Box::new(self), Box::new(other)],
649+
}
650+
}
651+
}
617652
#[cfg(test)]
618653
mod tests {
619654
use super::Expr;

0 commit comments

Comments
 (0)