Skip to content

Commit

Permalink
feat: in operator grpc finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Dec 17, 2024
1 parent 3f9c309 commit 2c18b72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
7 changes: 7 additions & 0 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ message MemberValue {
oneof value_type {
Primitive primitive = 1;
string string = 2;
MemberValueList list = 3;
}
}

message MemberValueList {
repeated MemberValue values = 1;
}

message MemberClause {
string model = 2;
string member = 3;
Expand Down Expand Up @@ -152,6 +157,8 @@ enum ComparisonOperator {
GTE = 3;
LT = 4;
LTE = 5;
IN = 6;
NOT_IN = 7;
}

message Token {
Expand Down
41 changes: 25 additions & 16 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,19 @@ impl DojoWorld {
let comparison_operator = ComparisonOperator::from_repr(member_clause.operator as usize)
.expect("invalid comparison operator");

let comparison_value =
match member_clause.value.ok_or(QueryError::MissingParam("value".into()))?.value_type {
Some(ValueType::String(value)) => value,
fn comparison_value(value: &proto::types::MemberValue) -> Result<String, Error> {
match &value.value_type {
Some(ValueType::String(value)) => Ok(value.to_string()),

Check warning on line 778 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L776-L778

Added lines #L776 - L778 were not covered by tests
Some(ValueType::Primitive(value)) => {
let primitive: Primitive = value.try_into()?;
primitive.to_sql_value()
let primitive: Primitive = (value.clone()).try_into()?;
Ok(primitive.to_sql_value())

Check warning on line 781 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L780-L781

Added lines #L780 - L781 were not covered by tests
}
Some(ValueType::List(values)) => {
Ok(format!("({})", values.values.iter().map(|v| comparison_value(v)).collect::<Result<Vec<String>, Error>>()?.join(", ")))

Check warning on line 784 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L783-L784

Added lines #L783 - L784 were not covered by tests
}
None => return Err(QueryError::MissingParam("value_type".into()).into()),
};
}
}

Check warning on line 788 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L788

Added line #L788 was not covered by tests

let (namespace, model) = member_clause
.model
Expand Down Expand Up @@ -838,14 +842,15 @@ impl DojoWorld {
offset,
)?;

let value = comparison_value(&member_clause.value.clone().ok_or(QueryError::MissingParam("value".into()))?)?;

Check warning on line 845 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L845

Added line #L845 was not covered by tests
let total_count = sqlx::query_scalar(&count_query)
.bind(comparison_value.clone())
.bind(value.clone())

Check warning on line 847 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L847

Added line #L847 was not covered by tests
.bind(entity_updated_after.clone())
.fetch_optional(&self.pool)
.await?
.unwrap_or(0);

let mut query = sqlx::query(&entity_query).bind(comparison_value);
let mut query = sqlx::query(&entity_query).bind(value);

Check warning on line 853 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L853

Added line #L853 was not covered by tests
if let Some(entity_updated_after) = entity_updated_after.clone() {
query = query.bind(entity_updated_after);
}
Expand Down Expand Up @@ -1356,17 +1361,21 @@ fn build_composite_clause(
ClauseType::Member(member) => {
let comparison_operator = ComparisonOperator::from_repr(member.operator as usize)
.expect("invalid comparison operator");
let value = member.value.clone();
let comparison_value =
match value.ok_or(QueryError::MissingParam("value".into()))?.value_type {
Some(ValueType::String(value)) => value,
let value = member.value.clone().ok_or(QueryError::MissingParam("value".into()))?;
fn comparison_value(value: &proto::types::MemberValue) -> Result<String, Error> {
match &value.value_type {
Some(ValueType::String(value)) => Ok(value.to_string()),

Check warning on line 1367 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1364-L1367

Added lines #L1364 - L1367 were not covered by tests
Some(ValueType::Primitive(value)) => {
let primitive: Primitive = value.try_into()?;
primitive.to_sql_value()
let primitive: Primitive = (value.clone()).try_into()?;
Ok(primitive.to_sql_value())

Check warning on line 1370 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1369-L1370

Added lines #L1369 - L1370 were not covered by tests
}
Some(ValueType::List(values)) => {
Ok(format!("({})", values.values.iter().map(|v| comparison_value(v)).collect::<Result<Vec<String>, Error>>()?.join(", ")))

Check warning on line 1373 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1372-L1373

Added lines #L1372 - L1373 were not covered by tests
}
None => return Err(QueryError::MissingParam("value_type".into()).into()),
};
bind_values.push(comparison_value);
}
}
bind_values.push(comparison_value(&value)?);

Check warning on line 1378 in crates/torii/grpc/src/server/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L1377-L1378

Added lines #L1377 - L1378 were not covered by tests

let model = member.model.clone();
// Get or create unique alias for this model
Expand Down
6 changes: 6 additions & 0 deletions crates/torii/grpc/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub enum ComparisonOperator {
Gte,
Lt,
Lte,
In,
NotIn,
}

impl fmt::Display for ComparisonOperator {
Expand All @@ -198,6 +200,8 @@ impl fmt::Display for ComparisonOperator {
ComparisonOperator::Lte => write!(f, "<="),
ComparisonOperator::Neq => write!(f, "!="),
ComparisonOperator::Eq => write!(f, "="),
ComparisonOperator::In => write!(f, "IN"),
ComparisonOperator::NotIn => write!(f, "NOT IN"),

Check warning on line 204 in crates/torii/grpc/src/types/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L203-L204

Added lines #L203 - L204 were not covered by tests
}
}
}
Expand All @@ -211,6 +215,8 @@ impl From<proto::types::ComparisonOperator> for ComparisonOperator {
proto::types::ComparisonOperator::Lt => ComparisonOperator::Lt,
proto::types::ComparisonOperator::Lte => ComparisonOperator::Lte,
proto::types::ComparisonOperator::Neq => ComparisonOperator::Neq,
proto::types::ComparisonOperator::In => ComparisonOperator::In,
proto::types::ComparisonOperator::NotIn => ComparisonOperator::NotIn,

Check warning on line 219 in crates/torii/grpc/src/types/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L218-L219

Added lines #L218 - L219 were not covered by tests
}
}
}
Expand Down

0 comments on commit 2c18b72

Please sign in to comment.