Skip to content

Commit

Permalink
feat: filtering bytearrays with member clause (#2425)
Browse files Browse the repository at this point in the history
* feat: filtering bytearrays with member clause

* fmt

* chore: proto devx
  • Loading branch information
Larkooo authored Sep 15, 2024
1 parent ed2aa83 commit 069bc1a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
9 changes: 8 additions & 1 deletion crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +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;
Primitive value = 5;
MemberValue value = 5;
}

message CompositeClause {
Expand Down
28 changes: 22 additions & 6 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_value::ValueType;
use crate::proto::world::world_server::WorldServer;
use crate::proto::world::{
SubscribeEntitiesRequest, SubscribeEntityResponse, SubscribeEventsResponse,
Expand Down Expand Up @@ -504,10 +505,15 @@ 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()))?.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
.model
Expand Down Expand Up @@ -619,8 +625,18 @@ 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()))?
.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);

Expand Down
22 changes: 20 additions & 2 deletions crates/torii/grpc/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet::core::types::{
};
use strum_macros::{AsRefStr, EnumIter, FromRepr};

use crate::proto::types::member_value;
use crate::proto::{self};

pub mod schema;
Expand Down Expand Up @@ -54,12 +55,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)]
Expand Down Expand Up @@ -284,7 +291,7 @@ impl From<MemberClause> 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()) }),
}
}
}
Expand All @@ -298,6 +305,17 @@ impl From<CompositeClause> for proto::types::CompositeClause {
}
}

impl From<MemberValue> for member_value::ValueType {
fn from(value: MemberValue) -> Self {
match value {
MemberValue::Primitive(primitive) => {
member_value::ValueType::Primitive(primitive.into())
}
MemberValue::String(string) => member_value::ValueType::String(string),
}
}
}

impl From<Value> for proto::types::Value {
fn from(value: Value) -> Self {
let value_type = match value.value_type {
Expand Down

0 comments on commit 069bc1a

Please sign in to comment.