Skip to content

Commit

Permalink
global arrays refactor & fix graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 29, 2024
1 parent 5233b5f commit 1de3f01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::str::FromStr;

use async_trait::async_trait;
use crypto_bigint::U256;
use dojo_types::primitive::Primitive;
use dojo_types::primitive::{Primitive, PrimitiveError};
use dojo_types::schema::Ty;
use dojo_world::contracts::abigen::model::Layout;
use dojo_world::contracts::model::ModelReader;
use serde_json::Value as JsonValue;
use sqlx::sqlite::SqliteRow;
use sqlx::{Pool, Row, Sqlite};
use starknet::core::types::Felt;
Expand Down Expand Up @@ -346,9 +347,19 @@ pub fn map_row_to_ty(
}
}
Ty::Array(ty) => {
let schema = ty[0].clone();
let serialized_array = row.try_get::<String, &str>(&column_name)?;

*ty = serde_json::from_str(&serialized_array).map_err(ParseError::FromJsonStr)?;
let values: Vec<JsonValue> =
serde_json::from_str(&serialized_array).map_err(ParseError::FromJsonStr)?;
*ty = values
.iter()
.map(|v| {
let mut ty = schema.clone();
ty.from_json_value(v.clone())?;
Result::<_, PrimitiveError>::Ok(ty)
})
.collect::<Result<Vec<Ty>, _>>()?;
}
Ty::ByteArray(bytearray) => {
let value = row.try_get::<String, &str>(&column_name)?;
Expand Down
4 changes: 3 additions & 1 deletion crates/torii/core/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,9 @@ impl Sql {
}
Ty::Array(array) => {
columns.push(format!("\"{}\"", prefix));
arguments.push(Argument::String(serde_json::to_string(array)?));
let values =
array.iter().map(|v| v.to_json_value()).collect::<Result<Vec<_>, _>>()?;
arguments.push(Argument::String(serde_json::to_string(&values)?));
}
Ty::Primitive(ty) => {
columns.push(format!("\"{}\"", prefix));
Expand Down

0 comments on commit 1de3f01

Please sign in to comment.