From bce48a41d65e362f502439724d999fe94480297a Mon Sep 17 00:00:00 2001 From: Nasr Date: Fri, 13 Sep 2024 18:09:43 -0400 Subject: [PATCH 1/3] feat: filtering bytearrays with member clause --- crates/torii/grpc/proto/types.proto | 5 ++++- crates/torii/grpc/src/server/mod.rs | 23 +++++++++++++++++------ crates/torii/grpc/src/types/mod.rs | 19 +++++++++++++++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/crates/torii/grpc/proto/types.proto b/crates/torii/grpc/proto/types.proto index 70b630159e..a69e5dbf64 100644 --- a/crates/torii/grpc/proto/types.proto +++ b/crates/torii/grpc/proto/types.proto @@ -116,7 +116,10 @@ message MemberClause { string model = 2; string member = 3; ComparisonOperator operator = 4; - Primitive value = 5; + oneof value { + Primitive primitive = 5; + string string = 6; + } } message CompositeClause { diff --git a/crates/torii/grpc/src/server/mod.rs b/crates/torii/grpc/src/server/mod.rs index 30054029c1..9168b00994 100644 --- a/crates/torii/grpc/src/server/mod.rs +++ b/crates/torii/grpc/src/server/mod.rs @@ -41,6 +41,7 @@ use self::subscriptions::entity::EntityManager; use self::subscriptions::event_message::EventMessageManager; use self::subscriptions::model_diff::{ModelDiffRequest, StateDiffManager}; use crate::proto::types::clause::ClauseType; +use crate::proto::types::member_clause; use crate::proto::world::world_server::WorldServer; use crate::proto::world::{ SubscribeEntitiesRequest, SubscribeEntityResponse, SubscribeEventsResponse, @@ -504,10 +505,14 @@ impl DojoWorld { let comparison_operator = ComparisonOperator::from_repr(member_clause.operator as usize) .expect("invalid comparison operator"); - let primitive: Primitive = - member_clause.value.ok_or(QueryError::MissingParam("value".into()))?.try_into()?; - - let comparison_value = primitive.to_sql_value()?; + let comparison_value = + match member_clause.value.ok_or(QueryError::MissingParam("value".into()))? { + member_clause::Value::String(value) => value, + member_clause::Value::Primitive(value) => { + let primitive: Primitive = value.try_into()?; + primitive.to_sql_value()? + } + }; let (namespace, model) = member_clause .model @@ -619,8 +624,14 @@ impl DojoWorld { let comparison_operator = ComparisonOperator::from_repr(member.operator as usize) .expect("invalid comparison operator"); - let value: Primitive = member.value.unwrap().try_into()?; - let comparison_value = value.to_sql_value()?; + let comparison_value = + match member.value.ok_or(QueryError::MissingParam("value".into()))? { + member_clause::Value::String(value) => value, + member_clause::Value::Primitive(value) => { + let primitive: Primitive = value.try_into()?; + primitive.to_sql_value()? + } + }; let column_name = format!("external_{}", member.member); diff --git a/crates/torii/grpc/src/types/mod.rs b/crates/torii/grpc/src/types/mod.rs index 290dfd4881..e61ad84ce1 100644 --- a/crates/torii/grpc/src/types/mod.rs +++ b/crates/torii/grpc/src/types/mod.rs @@ -11,7 +11,7 @@ use starknet::core::types::{ }; use strum_macros::{AsRefStr, EnumIter, FromRepr}; -use crate::proto::{self}; +use crate::proto::{self, types::member_clause}; pub mod schema; @@ -54,12 +54,18 @@ pub enum PatternMatching { VariableLen, } +#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] +pub enum MemberValue { + Primitive(Primitive), + String(String), +} + #[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] pub struct MemberClause { pub model: String, pub member: String, pub operator: ComparisonOperator, - pub value: Primitive, + pub value: MemberValue, } #[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] @@ -298,6 +304,15 @@ impl From for proto::types::CompositeClause { } } +impl From for member_clause::Value { + fn from(value: MemberValue) -> Self { + match value { + MemberValue::Primitive(primitive) => member_clause::Value::Primitive(primitive.into()), + MemberValue::String(string) => member_clause::Value::String(string), + } + } +} + impl From for proto::types::Value { fn from(value: Value) -> Self { let value_type = match value.value_type { From 9dddef69b1578dd1abd8fdec6948ec7dc2430242 Mon Sep 17 00:00:00 2001 From: Nasr Date: Fri, 13 Sep 2024 18:20:57 -0400 Subject: [PATCH 2/3] fmt --- crates/torii/grpc/src/types/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/torii/grpc/src/types/mod.rs b/crates/torii/grpc/src/types/mod.rs index e61ad84ce1..6272a633c2 100644 --- a/crates/torii/grpc/src/types/mod.rs +++ b/crates/torii/grpc/src/types/mod.rs @@ -11,7 +11,8 @@ use starknet::core::types::{ }; use strum_macros::{AsRefStr, EnumIter, FromRepr}; -use crate::proto::{self, types::member_clause}; +use crate::proto::types::member_clause; +use crate::proto::{self}; pub mod schema; From cbda51aa4167089ed0cdd4db21613aedc96d39be Mon Sep 17 00:00:00 2001 From: Nasr Date: Sun, 15 Sep 2024 12:41:48 -0400 Subject: [PATCH 3/3] chore: proto devx --- crates/torii/grpc/proto/types.proto | 12 ++++++++---- crates/torii/grpc/src/server/mod.rs | 29 +++++++++++++++++------------ crates/torii/grpc/src/types/mod.rs | 12 +++++++----- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/crates/torii/grpc/proto/types.proto b/crates/torii/grpc/proto/types.proto index a69e5dbf64..deb84032cd 100644 --- a/crates/torii/grpc/proto/types.proto +++ b/crates/torii/grpc/proto/types.proto @@ -112,14 +112,18 @@ message HashedKeysClause { repeated bytes hashed_keys = 1; } +message MemberValue { + oneof value_type { + Primitive primitive = 1; + string string = 2; + } +} + message MemberClause { string model = 2; string member = 3; ComparisonOperator operator = 4; - oneof value { - Primitive primitive = 5; - string string = 6; - } + MemberValue value = 5; } message CompositeClause { diff --git a/crates/torii/grpc/src/server/mod.rs b/crates/torii/grpc/src/server/mod.rs index 9168b00994..3eb8cf035c 100644 --- a/crates/torii/grpc/src/server/mod.rs +++ b/crates/torii/grpc/src/server/mod.rs @@ -41,7 +41,7 @@ use self::subscriptions::entity::EntityManager; use self::subscriptions::event_message::EventMessageManager; use self::subscriptions::model_diff::{ModelDiffRequest, StateDiffManager}; use crate::proto::types::clause::ClauseType; -use crate::proto::types::member_clause; +use crate::proto::types::member_value::ValueType; use crate::proto::world::world_server::WorldServer; use crate::proto::world::{ SubscribeEntitiesRequest, SubscribeEntityResponse, SubscribeEventsResponse, @@ -506,12 +506,13 @@ impl DojoWorld { .expect("invalid comparison operator"); let comparison_value = - match member_clause.value.ok_or(QueryError::MissingParam("value".into()))? { - member_clause::Value::String(value) => value, - member_clause::Value::Primitive(value) => { + match member_clause.value.ok_or(QueryError::MissingParam("value".into()))?.value_type { + Some(ValueType::String(value)) => value, + Some(ValueType::Primitive(value)) => { let primitive: Primitive = value.try_into()?; primitive.to_sql_value()? } + None => return Err(QueryError::MissingParam("value_type".into()).into()), }; let (namespace, model) = member_clause @@ -624,14 +625,18 @@ impl DojoWorld { let comparison_operator = ComparisonOperator::from_repr(member.operator as usize) .expect("invalid comparison operator"); - let comparison_value = - match member.value.ok_or(QueryError::MissingParam("value".into()))? { - member_clause::Value::String(value) => value, - member_clause::Value::Primitive(value) => { - let primitive: Primitive = value.try_into()?; - primitive.to_sql_value()? - } - }; + let comparison_value = match member + .value + .ok_or(QueryError::MissingParam("value".into()))? + .value_type + { + Some(ValueType::String(value)) => value, + Some(ValueType::Primitive(value)) => { + let primitive: Primitive = value.try_into()?; + primitive.to_sql_value()? + } + None => return Err(QueryError::MissingParam("value_type".into()).into()), + }; let column_name = format!("external_{}", member.member); diff --git a/crates/torii/grpc/src/types/mod.rs b/crates/torii/grpc/src/types/mod.rs index 6272a633c2..fad16b3739 100644 --- a/crates/torii/grpc/src/types/mod.rs +++ b/crates/torii/grpc/src/types/mod.rs @@ -11,7 +11,7 @@ use starknet::core::types::{ }; use strum_macros::{AsRefStr, EnumIter, FromRepr}; -use crate::proto::types::member_clause; +use crate::proto::types::member_value; use crate::proto::{self}; pub mod schema; @@ -291,7 +291,7 @@ impl From for proto::types::MemberClause { model: value.model, member: value.member, operator: value.operator as i32, - value: Some(value.value.into()), + value: Some(proto::types::MemberValue { value_type: Some(value.value.into()) }), } } } @@ -305,11 +305,13 @@ impl From for proto::types::CompositeClause { } } -impl From for member_clause::Value { +impl From for member_value::ValueType { fn from(value: MemberValue) -> Self { match value { - MemberValue::Primitive(primitive) => member_clause::Value::Primitive(primitive.into()), - MemberValue::String(string) => member_clause::Value::String(string), + MemberValue::Primitive(primitive) => { + member_value::ValueType::Primitive(primitive.into()) + } + MemberValue::String(string) => member_value::ValueType::String(string), } } }