Skip to content

Commit

Permalink
generate the model/field selector value instead of selector!(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed May 9, 2024
1 parent ac3697b commit 9fe788a
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 378 deletions.
25 changes: 15 additions & 10 deletions crates/dojo-lang/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use dojo_world::manifest::Member;
use itertools::Itertools;
use starknet::core::utils::get_selector_from_name;

#[derive(PartialEq)]
enum CompositeType {
Expand Down Expand Up @@ -673,11 +674,12 @@ fn handle_introspect_internal(

layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: \
dojo::database::introspect::Layout::Fixed(array![{}].span())
}}",
m.name, values
get_selector_from_name(m.name.as_str()).unwrap(),
values
))
}
};
Expand All @@ -698,10 +700,10 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: dojo::database::introspect::Layout::ByteArray
}}",
m.name
get_selector_from_name(m.name.as_str()).unwrap()
));
}
}
Expand All @@ -728,10 +730,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: {}
}}",
m.name, array_layout
get_selector_from_name(m.name.as_str()).unwrap(),
array_layout
));
}
}
Expand All @@ -755,10 +758,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: {}
}}",
m.name, tuple_layout
get_selector_from_name(m.name.as_str()).unwrap(),
tuple_layout
));
}
}
Expand All @@ -783,10 +787,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: dojo::database::introspect::Introspect::<{}>::layout()
}}",
m.name, m.ty
get_selector_from_name(m.name.as_str()).unwrap(),
m.ty
));
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/dojo-lang/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use convert_case::{Case, Casing};
use dojo_world::manifest::Member;
use starknet::core::utils::get_selector_from_name;

use crate::plugin::{DojoAuxData, Model, DOJO_MODEL_ATTR};

Expand Down Expand Up @@ -233,7 +234,7 @@ pub fn handle_model_struct(
0 => (RewriteNode::Text("0".to_string()), RewriteNode::Text(format!("\"{model_name}\""))),
_ => (
RewriteNode::Text(DEFAULT_MODEL_VERSION.to_string()),
RewriteNode::Text(format!("selector!(\"{model_name}\")")),
RewriteNode::Text(get_selector_from_name(model_name.as_str()).unwrap().to_string()),
),
};

Expand Down Expand Up @@ -310,7 +311,7 @@ pub fn handle_model_struct(
impl $type_name$Model of dojo::model::Model<$type_name$> {
fn entity(world: dojo::world::IWorldDispatcher, keys: Span<felt252>, layout: \
dojo::database::introspect::Layout) -> $type_name$ {
let values = dojo::world::IWorldDispatcherTrait::entity(world, selector!(\"$type_name$\"), \
let values = dojo::world::IWorldDispatcherTrait::entity(world, $model_selector$, \
keys, layout);
// TODO: Generate method to deserialize from keys / values directly to avoid
Expand Down
Loading

0 comments on commit 9fe788a

Please sign in to comment.