Skip to content

Commit

Permalink
map rows to proto model
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Nov 20, 2023
1 parent 0a33c58 commit 2cb0bf5
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 75 deletions.
28 changes: 14 additions & 14 deletions crates/dojo-types/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ macro_rules! set_primitive {
}

/// Macro to generate getter methods for Primitive enum variants.
macro_rules! get_primitive {
macro_rules! as_primitive {
($method_name:ident, $variant:ident, $type:ty) => {
/// If the `Primitive` is type T, returns the associated [`T`]. Returns `None` otherwise.
pub fn $method_name(&self) -> Option<$type> {
Expand All @@ -76,17 +76,17 @@ macro_rules! get_primitive {
}

impl Primitive {
get_primitive!(as_u8, U8, u8);
get_primitive!(as_u16, U16, u16);
get_primitive!(as_u32, U32, u32);
get_primitive!(as_u64, U64, u64);
get_primitive!(as_u128, U128, u128);
get_primitive!(as_u256, U256, U256);
get_primitive!(as_bool, Bool, bool);
get_primitive!(as_usize, USize, u32);
get_primitive!(as_felt252, Felt252, FieldElement);
get_primitive!(as_class_hash, ClassHash, FieldElement);
get_primitive!(as_contract_address, ContractAddress, FieldElement);
as_primitive!(as_u8, U8, u8);
as_primitive!(as_u16, U16, u16);
as_primitive!(as_u32, U32, u32);
as_primitive!(as_u64, U64, u64);
as_primitive!(as_u128, U128, u128);
as_primitive!(as_u256, U256, U256);
as_primitive!(as_bool, Bool, bool);
as_primitive!(as_usize, USize, u32);
as_primitive!(as_felt252, Felt252, FieldElement);
as_primitive!(as_class_hash, ClassHash, FieldElement);
as_primitive!(as_contract_address, ContractAddress, FieldElement);

set_primitive!(set_u8, U8, u8);
set_primitive!(set_u16, U16, u16);
Expand Down Expand Up @@ -285,8 +285,8 @@ mod tests {
let mut deserialized = primitive;
deserialized.deserialize(&mut serialized.clone()).unwrap();

assert_eq!(sql_value, "0xaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd");
assert_eq!(
assert_eq!(sql_value,
"0xaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd"); assert_eq!(
serialized,
vec![
FieldElement::from_str("0xccccccccccccccccdddddddddddddddd").unwrap(),
Expand Down
10 changes: 5 additions & 5 deletions crates/torii/core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl ModelCache {
pub async fn schema(&self, model: &str) -> Result<Arc<SchemaData>, Error> {
{
let schemas = self.schemas.read().await;
if let Some(schema_data) = schemas.get(model) {
return Ok(Arc::clone(schema_data));
if let Some(schema) = schemas.get(model) {
return Ok(Arc::clone(schema));
}
}

Expand All @@ -49,12 +49,12 @@ impl ModelCache {

let ty = parse_sql_model_members(model, &model_members);
let sql = build_sql_model_query(ty.as_struct().unwrap());
let schema_data = Arc::new(SchemaData { ty, sql });
let schema = Arc::new(SchemaData { ty, sql });

let mut schemas = self.schemas.write().await;
schemas.insert(model.into(), Arc::clone(&schema_data));
schemas.insert(model.into(), Arc::clone(&schema));

Ok(schema_data)
Ok(schema)
}

pub async fn clear(&self) {
Expand Down
60 changes: 30 additions & 30 deletions crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub fn parse_sql_model_members(model: &str, model_members_all: &[SqlModelMember]
parse_sql_model_members_impl(model, model_members_all)
}

/// A helper function to build a model query including all nested structs
/// A helper function to build a model query including all nested structs and its the entity id
pub fn build_sql_model_query(schema: &Struct) -> String {
fn build_sql_model_query_impl(
path: &str,
Expand Down Expand Up @@ -198,81 +198,81 @@ pub fn build_sql_model_query(schema: &Struct) -> String {
}

/// Converts SQLite rows into a vector of `Ty` based on a specified schema.
pub fn map_rows_to_tys(schema: &Struct, rows: &[SqliteRow]) -> Result<Vec<Ty>, Error> {
pub fn map_rows_to_tys(schema: &Ty, rows: &[SqliteRow]) -> Result<Vec<Ty>, Error> {
fn populate_struct_from_row(
path: &str,
struct_ty: &mut Struct,
row: &SqliteRow,
) -> Result<(), Error> {
for child in struct_ty.children.iter_mut() {
let column_name = format!("{}.{}", path, child.name);
match &mut child.ty {
Ty::Primitive(p) => {
match &p {
for member in struct_ty.children.iter_mut() {
let column_name = format!("{}.{}", path, member.name);
match &mut member.ty {
Ty::Primitive(primitive) => {
match &primitive {
Primitive::Bool(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_bool(Some(value == 1))?;
let value = row.try_get::<bool, &str>(&column_name)?;
primitive.set_bool(Some(value))?;
}
Primitive::USize(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_usize(Some(value as u32))?;
let value = row.try_get::<u32, &str>(&column_name)?;
primitive.set_usize(Some(value))?;
}
Primitive::U8(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_u8(Some(value as u8))?;
let value = row.try_get::<u8, &str>(&column_name)?;
primitive.set_u8(Some(value))?;
}
Primitive::U16(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_u16(Some(value as u16))?;
let value = row.try_get::<u16, &str>(&column_name)?;
primitive.set_u16(Some(value))?;
}
Primitive::U32(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_u32(Some(value as u32))?;
let value = row.try_get::<u32, &str>(&column_name)?;
primitive.set_u32(Some(value))?;
}
Primitive::U64(_) => {
let value = row.try_get::<i64, &str>(&column_name)?;
p.set_u64(Some(value as u64))?;
primitive.set_u64(Some(value as u64))?;
}
Primitive::U128(_) => {
let value = row.try_get::<String, &str>(&column_name)?;
let hex_str = value.trim_start_matches("0x");
p.set_u128(Some(
primitive.set_u128(Some(
u128::from_str_radix(hex_str, 16)
.map_err(ParseError::ParseIntError)?,
))?;
}
Primitive::U256(_) => {
let value = row.try_get::<String, &str>(&column_name)?;
let hex_str = value.trim_start_matches("0x");
p.set_u256(Some(U256::from_be_hex(hex_str)))?;
primitive.set_u256(Some(U256::from_be_hex(hex_str)))?;
}
Primitive::Felt252(_) => {
let value = row.try_get::<String, &str>(&column_name)?;
p.set_felt252(Some(
primitive.set_felt252(Some(
FieldElement::from_str(&value).map_err(ParseError::FromStr)?,
))?;
}
Primitive::ClassHash(_) => {
let value = row.try_get::<String, &str>(&column_name)?;
p.set_class_hash(Some(
primitive.set_class_hash(Some(
FieldElement::from_str(&value).map_err(ParseError::FromStr)?,
))?;
}
Primitive::ContractAddress(_) => {
let value = row.try_get::<String, &str>(&column_name)?;
p.set_contract_address(Some(
primitive.set_contract_address(Some(
FieldElement::from_str(&value).map_err(ParseError::FromStr)?,
))?;
}
};
}
Ty::Enum(e) => {
Ty::Enum(enum_ty) => {
let value = row.try_get::<String, &str>(&column_name)?;
e.set_option(&value)?;
enum_ty.set_option(&value)?;
}
Ty::Struct(nested) => {
let path = [path, &nested.name].join("$");
populate_struct_from_row(&path, nested, row)?;
Ty::Struct(struct_ty) => {
let path = [path, &struct_ty.name].join("$");
populate_struct_from_row(&path, struct_ty, row)?;
}
ty => {
unimplemented!("unimplemented type_enum: {ty}");
Expand All @@ -285,8 +285,8 @@ pub fn map_rows_to_tys(schema: &Struct, rows: &[SqliteRow]) -> Result<Vec<Ty>, E

rows.iter()
.map(|row| {
let mut struct_ty = schema.clone();
populate_struct_from_row(&schema.name, &mut struct_ty, row)?;
let mut struct_ty = schema.as_struct().expect("schema should be struct ty").clone();
populate_struct_from_row(&schema.name(), &mut struct_ty, row)?;

Ok(Ty::Struct(struct_ty))
})
Expand Down
12 changes: 10 additions & 2 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ message ModelMetadata {
bytes schema = 6;
}

message Enum {
// Variant
uint32 option = 1;
// Variants of the enum
repeated string options = 2;
}

message Member {
// Name of the member
string name = 1;
// Type of value
oneof value_type {
oneof member_type {
Value value = 2;
Model struct = 3;
Enum enum = 3;
Model struct = 4;
}
}

Expand Down
Loading

0 comments on commit 2cb0bf5

Please sign in to comment.