Skip to content

Commit

Permalink
refactor(torii-grpc): use Option for felt as keys wildcard (#2119)
Browse files Browse the repository at this point in the history
* refactor: use Option for felt as wildcard

* reduce code footprint

* clippy😡

* fmt🤬
  • Loading branch information
Larkooo authored Jun 28, 2024
1 parent 6193f22 commit ed0c677
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
15 changes: 6 additions & 9 deletions crates/torii/grpc/src/server/subscriptions/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions crates/torii/grpc/src/server/subscriptions/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions crates/torii/grpc/src/server/subscriptions/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions crates/torii/grpc/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct ModelKeysClause {

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct KeysClause {
pub keys: Vec<FieldElement>,
pub keys: Vec<Option<FieldElement>>,
pub pattern_matching: PatternMatching,
pub models: Vec<String>,
}
Expand Down Expand Up @@ -184,7 +184,11 @@ impl From<proto::types::PatternMatching> for PatternMatching {
impl From<KeysClause> 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,
}
Expand All @@ -198,8 +202,10 @@ impl TryFrom<proto::types::KeysClause> for KeysClause {
let keys = value
.keys
.iter()
.map(|k| FieldElement::from_byte_slice_be(k))
.collect::<Result<Vec<_>, _>>()?;
.map(|k| {
if k.is_empty() { Ok(None) } else { Ok(Some(FieldElement::from_byte_slice_be(k)?)) }
})
.collect::<Result<Vec<Option<FieldElement>>, _>>()?;

Ok(Self { keys, pattern_matching: value.pattern_matching().into(), models: value.models })
}
Expand Down

0 comments on commit ed0c677

Please sign in to comment.