From ed0c67737996d24f3cd326fd360d7aec67a18c9c Mon Sep 17 00:00:00 2001 From: Larko <59736843+Larkooo@users.noreply.github.com> Date: Fri, 28 Jun 2024 17:57:39 -0400 Subject: [PATCH] refactor(torii-grpc): use Option for felt as keys wildcard (#2119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: use Option for felt as wildcard * reduce code footprint * clippy😡 * fmt🤬 --- .../torii/grpc/src/server/subscriptions/entity.rs | 15 ++++++--------- .../torii/grpc/src/server/subscriptions/event.rs | 15 ++++++--------- .../src/server/subscriptions/event_message.rs | 15 ++++++--------- crates/torii/grpc/src/types/mod.rs | 14 ++++++++++---- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/crates/torii/grpc/src/server/subscriptions/entity.rs b/crates/torii/grpc/src/server/subscriptions/entity.rs index a62ee8dd69..b93385ad07 100644 --- a/crates/torii/grpc/src/server/subscriptions/entity.rs +++ b/crates/torii/grpc/src/server/subscriptions/entity.rs @@ -138,17 +138,14 @@ impl Service { let sub_key = clause.keys.get(idx); match sub_key { - Some(sub_key) => { - if sub_key == &FieldElement::ZERO { - true - } else { - key == sub_key - } - } - // we overflowed the subscriber key pattern + // the key in the subscriber must match the key of the entity + // athis index + Some(Some(sub_key)) => key == sub_key, + // otherwise, if we have no key we should automatically match. + // or.. we overflowed the subscriber key pattern // but we're in VariableLen pattern matching // so we should match all next keys - None => true, + _ => true, } }) { continue; diff --git a/crates/torii/grpc/src/server/subscriptions/event.rs b/crates/torii/grpc/src/server/subscriptions/event.rs index 6a0aa38924..856f2211e6 100644 --- a/crates/torii/grpc/src/server/subscriptions/event.rs +++ b/crates/torii/grpc/src/server/subscriptions/event.rs @@ -104,17 +104,14 @@ impl Service { // if we have a key in the subscriber, it must match the key in the event // unless its empty, which is a wildcard match sub_key { - Some(sub_key) => { - if sub_key == &FieldElement::ZERO { - true - } else { - key == sub_key - } - } - // we overflowed the subscriber key pattern + // the key in the subscriber must match the key of the entity + // athis index + Some(Some(sub_key)) => key == sub_key, + // otherwise, if we have no key we should automatically match. + // or.. we overflowed the subscriber key pattern // but we're in VariableLen pattern matching // so we should match all next keys - None => true, + _ => true, } }) { continue; diff --git a/crates/torii/grpc/src/server/subscriptions/event_message.rs b/crates/torii/grpc/src/server/subscriptions/event_message.rs index 0b4b490ded..b345710bf0 100644 --- a/crates/torii/grpc/src/server/subscriptions/event_message.rs +++ b/crates/torii/grpc/src/server/subscriptions/event_message.rs @@ -137,17 +137,14 @@ impl Service { let sub_key = clause.keys.get(idx); match sub_key { - Some(sub_key) => { - if sub_key == &FieldElement::ZERO { - true - } else { - key == sub_key - } - } - // we overflowed the subscriber key pattern + // the key in the subscriber must match the key of the entity + // athis index + Some(Some(sub_key)) => key == sub_key, + // otherwise, if we have no key we should automatically match. + // or.. we overflowed the subscriber key pattern // but we're in VariableLen pattern matching // so we should match all next keys - None => true, + _ => true, } }) { continue; diff --git a/crates/torii/grpc/src/types/mod.rs b/crates/torii/grpc/src/types/mod.rs index cf00ede93f..3840daa1f0 100644 --- a/crates/torii/grpc/src/types/mod.rs +++ b/crates/torii/grpc/src/types/mod.rs @@ -43,7 +43,7 @@ pub struct ModelKeysClause { #[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)] pub struct KeysClause { - pub keys: Vec, + pub keys: Vec>, pub pattern_matching: PatternMatching, pub models: Vec, } @@ -184,7 +184,11 @@ impl From for PatternMatching { impl From for proto::types::KeysClause { fn from(value: KeysClause) -> Self { Self { - keys: value.keys.iter().map(|k| k.to_bytes_be().into()).collect(), + keys: value + .keys + .iter() + .map(|k| k.map_or(Vec::new(), |k| k.to_bytes_be().into())) + .collect(), pattern_matching: value.pattern_matching as i32, models: value.models, } @@ -198,8 +202,10 @@ impl TryFrom for KeysClause { let keys = value .keys .iter() - .map(|k| FieldElement::from_byte_slice_be(k)) - .collect::, _>>()?; + .map(|k| { + if k.is_empty() { Ok(None) } else { Ok(Some(FieldElement::from_byte_slice_be(k)?)) } + }) + .collect::>, _>>()?; Ok(Self { keys, pattern_matching: value.pattern_matching().into(), models: value.models }) }