Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filtering bytearrays with member clause #2425

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::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 @@
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()?

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L508-L513

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L515 was not covered by tests
};
Comment on lines +508 to +516
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohayo sensei! Consider refactoring the duplicated code into a separate function.

The code segments at line ranges 508-516 and 628-639 are very similar. They both match on value.value_type to extract the comparison value. To improve code reuse and maintainability, consider extracting the common logic into a separate function. Here's a suggestion:

fn extract_comparison_value(value: &proto::types::Value) -> Result<String, Error> {
    match value.value_type {
        Some(ValueType::String(value)) => Ok(value),
        Some(ValueType::Primitive(value)) => {
            let primitive: Primitive = value.try_into()?;
            Ok(primitive.to_sql_value()?)
        }
        None => Err(QueryError::MissingParam("value_type".into()).into()),
    }
}

Then, you can replace the duplicated code with calls to this function:

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

This will make the code more DRY and easier to maintain. What do you think, sensei?

Also applies to: 628-639


let (namespace, model) = member_clause
.model
Expand Down Expand Up @@ -619,8 +625,18 @@
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()))?

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L628-L630

Added lines #L628 - L630 were not covered by tests
.value_type
{
Some(ValueType::String(value)) => value,
Some(ValueType::Primitive(value)) => {
let primitive: Primitive = value.try_into()?;
primitive.to_sql_value()?

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/mod.rs#L633-L636

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L638 was not covered by tests
};

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 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 @@
VariableLen,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L58

Added line #L58 was not covered by tests
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 @@
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()) }),

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L294

Added line #L294 was not covered by tests
}
}
}
Expand All @@ -298,6 +305,17 @@
}
}

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

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L309-L312

Added lines #L309 - L312 were not covered by tests
}
MemberValue::String(string) => member_value::ValueType::String(string),

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L314

Added line #L314 was not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/types/mod.rs#L316

Added line #L316 was not covered by tests
}

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