Skip to content

Commit

Permalink
tag cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 20, 2024
1 parent 680957d commit d24261d
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
5 changes: 3 additions & 2 deletions crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::str::FromStr;

use async_trait::async_trait;
use crypto_bigint::U256;
use dojo_types::naming::get_tag;
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use dojo_world::contracts::abigen::model::Layout;
Expand Down Expand Up @@ -210,10 +211,10 @@ pub fn parse_sql_model_members(
}

Ty::Struct(Struct {
name: format!("{}-{}", namespace, model),
name: get_tag(namespace, model),
children: model_members_all
.iter()
.filter(|m| m.id == format!("{}-{}", namespace, model))
.filter(|m| m.id == get_tag(namespace, model))
.map(|m| Member {
key: m.key,
name: m.name.to_owned(),
Expand Down
6 changes: 2 additions & 4 deletions crates/torii/core/src/processors/event_message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{Error, Result};
use async_trait::async_trait;
use dojo_world::contracts::abigen::world::Event as WorldEvent;
use dojo_world::contracts::naming::get_tag;
use dojo_world::contracts::world::WorldContractReader;
use starknet::core::types::{Event, Felt};
use starknet::providers::Provider;
Expand Down Expand Up @@ -65,15 +66,12 @@ where
"Store event message."
);

// TODO: check historical and keep the internal counter.

let mut keys_and_unpacked = [event.keys, event.values].concat();

let mut entity = model.schema.clone();
entity.deserialize(&mut keys_and_unpacked)?;

// TODO: this must come from some torii's configuration.
let historical = config.is_historical(&format!("{}-{}", model.namespace, model.name));
let historical = config.is_historical(&get_tag(&model.namespace, &model.name));
db.set_event_message(entity, event_id, block_timestamp, historical).await?;
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion crates/torii/core/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use dojo_types::naming::get_tag;
use dojo_types::primitive::Primitive;
use dojo_types::schema::{EnumOption, Member, Struct, Ty};
use dojo_world::config::WorldMetadata;
Expand Down Expand Up @@ -257,7 +258,7 @@ impl Sql {
upgrade_diff: Option<&Ty>,
) -> Result<()> {
let selector = compute_selector_from_names(namespace, &model.name());
let namespaced_name = format!("{}-{}", namespace, model.name());
let namespaced_name = get_tag(namespace, &model.name());

let insert_models =
"INSERT INTO models (id, namespace, name, class_hash, contract_address, layout, \
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use async_graphql::dynamic::{
};
use async_graphql::{Name, Value};
use async_recursion::async_recursion;
use dojo_types::naming::get_tag;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use tokio_stream::StreamExt;
Expand All @@ -21,7 +22,6 @@ use crate::object::{resolve_many, resolve_one};
use crate::query::{type_mapping_query, value_mapping_from_row};
use crate::types::TypeData;
use crate::utils;

#[derive(Debug)]
pub struct EntityObject;

Expand Down Expand Up @@ -145,7 +145,7 @@ fn model_union_field() -> Field {
let data: ValueMapping = match model_data_recursive_query(
&mut conn,
ENTITY_ID_COLUMN,
vec![format!("{namespace}-{name}")],
vec![get_tag(&namespace, &name)],
&entity_id,
&[],
&type_mapping,
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use async_graphql::dynamic::{
Field, FieldFuture, FieldValue, InputValue, SubscriptionField, SubscriptionFieldFuture, TypeRef,
};
use async_graphql::{Name, Value};
use dojo_types::naming::get_tag;
use sqlx::{Pool, Sqlite};
use tokio_stream::StreamExt;
use torii_core::simple_broker::SimpleBroker;
Expand All @@ -19,7 +20,6 @@ use crate::mapping::ENTITY_TYPE_MAPPING;
use crate::object::{resolve_many, resolve_one};
use crate::query::type_mapping_query;
use crate::utils;

#[derive(Debug)]
pub struct EventMessageObject;

Expand Down Expand Up @@ -151,7 +151,7 @@ fn model_union_field() -> Field {
let data: ValueMapping = match model_data_recursive_query(
&mut conn,
EVENT_MESSAGE_ID_COLUMN,
vec![format!("{namespace}-{name}")],
vec![get_tag(&namespace, &name)],

Check warning on line 154 in crates/torii/graphql/src/object/event_message.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/graphql/src/object/event_message.rs#L154

Added line #L154 was not covered by tests
&entity_id,
&[],
&type_mapping,
Expand Down
9 changes: 5 additions & 4 deletions crates/torii/graphql/src/object/model_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_graphql::dynamic::{Enum, Field, FieldFuture, InputObject, Object, TypeRef};
use async_graphql::Value;
use chrono::{DateTime, Utc};
use dojo_types::naming::get_tag;
use serde::Deserialize;
use sqlx::{FromRow, Pool, Sqlite};

Expand Down Expand Up @@ -69,7 +70,7 @@ impl BasicObject for ModelDataObject {
let mut parts = self.type_name().split('_').collect::<Vec<&str>>();
let model = parts.pop().unwrap();
let namespace = parts.join("_");
let type_name = utils::struct_name_from_names(&namespace, model);
let type_name = get_tag(&namespace, model);
let mut objects = data_objects_recursion(
&TypeData::Nested((TypeRef::named(self.type_name()), self.type_mapping.clone())),
&vec![type_name],
Expand Down Expand Up @@ -106,18 +107,18 @@ impl ResolvableObject for ModelDataObject {
let mut parts = type_name.split('_').collect::<Vec<&str>>();
let model = parts.pop().unwrap();
let namespace = parts.join("_");
let type_name = utils::struct_name_from_names(&namespace, model);
let table_name = get_tag(&namespace, model);

FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let order = parse_order_argument(&ctx);
let filters = parse_where_argument(&ctx, &where_mapping)?;
let connection = parse_connection_arguments(&ctx)?;

let total_count = count_rows(&mut conn, &type_name, &None, &filters).await?;
let total_count = count_rows(&mut conn, &table_name, &None, &filters).await?;
let (data, page_info) = fetch_multiple_rows(
&mut conn,
&type_name,
&table_name,
EVENT_ID_COLUMN,
&None,
&order,
Expand Down
5 changes: 3 additions & 2 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod tests {
use std::time::Duration;

use async_graphql::value;
use dojo_types::naming::get_tag;
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use dojo_world::contracts::abigen::model::Layout;
Expand Down Expand Up @@ -81,7 +82,7 @@ mod tests {
// 1. Open process and sleep.Go to execute subscription
tokio::time::sleep(Duration::from_secs(1)).await;
let ty = Ty::Struct(Struct {
name: utils::struct_name_from_names(&namespace, &model_name),
name: get_tag(&namespace, &model_name),
children: vec![
Member {
name: "depth".to_string(),
Expand Down Expand Up @@ -233,7 +234,7 @@ mod tests {
// 1. Open process and sleep.Go to execute subscription
tokio::time::sleep(Duration::from_secs(1)).await;
let ty = Ty::Struct(Struct {
name: utils::struct_name_from_names(&namespace, &model_name),
name: get_tag(&namespace, &model_name),
children: vec![
Member {
name: "depth".to_string(),
Expand Down
4 changes: 0 additions & 4 deletions crates/torii/graphql/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ pub fn field_name_from_names(namespace: &str, model_name: &str) -> String {
pub fn type_name_from_names(namespace: &str, model_name: &str) -> String {
format!("{}_{}", namespace, model_name)
}

pub fn struct_name_from_names(namespace: &str, model_name: &str) -> String {
format!("{}-{}", namespace, model_name)
}

0 comments on commit d24261d

Please sign in to comment.