Skip to content

Commit

Permalink
fix: ensure models generated Store trait exposes set/update/delete (#…
Browse files Browse the repository at this point in the history
…2348)

* fix: ensure the store traits are exposing high level functions for get/set/update

* fix: fix tests

* fix: fix test by using explicit store trait
  • Loading branch information
glihm authored Aug 27, 2024
1 parent b2ce5ef commit e1a349b
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 14 deletions.
4 changes: 2 additions & 2 deletions crates/dojo-core/src/model/metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ pub impl ResourceMetadataModel of Model<ResourceMetadata> {
ResourceMetadataTrait::from_values(*keys.at(0), ref values)
}

fn set(self: @ResourceMetadata, world: IWorldDispatcher,) {
fn set_model(self: @ResourceMetadata, world: IWorldDispatcher,) {
IWorldDispatcherTrait::set_entity(
world, Self::selector(), ModelIndex::Keys(self.keys()), self.values(), Self::layout()
);
}

fn delete(self: @ResourceMetadata, world: IWorldDispatcher,) {
fn delete_model(self: @ResourceMetadata, world: IWorldDispatcher,) {
world.delete_entity(Self::selector(), ModelIndex::Keys(self.keys()), Self::layout());
}

Expand Down
15 changes: 11 additions & 4 deletions crates/dojo-core/src/model/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@ pub trait ModelEntity<T> {
fn id(self: @T) -> felt252;
fn values(self: @T) -> Span<felt252>;
fn from_values(entity_id: felt252, ref values: Span<felt252>) -> T;
// Get is always used with the trait path, which results in no ambiguity for the compiler.
fn get(world: IWorldDispatcher, entity_id: felt252) -> T;
fn update(self: @T, world: IWorldDispatcher);
fn delete(self: @T, world: IWorldDispatcher);
// Update and delete can be used directly on the entity, which results in ambiguity.
// Therefore, they are implemented with the `update_entity` and `delete_entity` names.
fn update_entity(self: @T, world: IWorldDispatcher);
fn delete_entity(self: @T, world: IWorldDispatcher);
fn get_member(
world: IWorldDispatcher, entity_id: felt252, member_id: felt252,
) -> Span<felt252>;
fn set_member(self: @T, world: IWorldDispatcher, member_id: felt252, values: Span<felt252>);
}

pub trait Model<T> {
// Get is always used with the trait path, which results in no ambiguity for the compiler.
fn get(world: IWorldDispatcher, keys: Span<felt252>) -> T;
// Note: `get` is implemented with a generated trait because it takes
// the list of model keys as separated parameters.
fn set(self: @T, world: IWorldDispatcher);
fn delete(self: @T, world: IWorldDispatcher);

// Set and delete can be used directly on the entity, which results in ambiguity.
// Therefore, they are implemented with the `set_model` and `delete_model` names.
fn set_model(self: @T, world: IWorldDispatcher);
fn delete_model(self: @T, world: IWorldDispatcher);

fn get_member(
world: IWorldDispatcher, keys: Span<felt252>, member_id: felt252,
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/tests/benchmarks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use dojo::model::introspect::Introspect;
use dojo::storage::{database, storage};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use dojo::tests::helpers::{Foo, Sword, Case, case, Character, Abilities, Stats, Weapon};
use dojo::tests::helpers::{Foo, Sword, Case, CaseStore, case, Character, Abilities, Stats, Weapon};
use dojo::utils::test::{spawn_test_world, GasCounterTrait};

#[derive(Drop, Serde)]
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/inline_macros/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl InlineMacroExprPlugin for DeleteMacro {
builder.add_str(&format!(
"
let __delete_model_instance__ = {};
dojo::model::Model::delete(@__delete_model_instance__, {});
dojo::model::Model::delete_model(@__delete_model_instance__, {});
",
entity,
world.as_syntax_node().get_text(db),
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/inline_macros/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl InlineMacroExprPlugin for SetMacro {
builder.add_str(&format!(
"
let __set_model_instance__ = {};
dojo::model::Model::set(@__set_model_instance__, {});
dojo::model::Model::set_model(@__set_model_instance__, {});
",
entity,
world.as_syntax_node().get_text(db),
Expand Down
24 changes: 20 additions & 4 deletions crates/dojo-lang/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ pub impl $type_name$EntityStoreImpl of $type_name$EntityStore {
$type_name$ModelEntityImpl::get(world, entity_id)
}
fn update(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
dojo::model::ModelEntity::<$type_name$Entity>::update_entity(self, world);
}
fn delete(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
dojo::model::ModelEntity::<$type_name$Entity>::delete_entity(self, world);
}
$entity_field_accessors$
}
Expand Down Expand Up @@ -368,6 +376,14 @@ pub impl $type_name$StoreImpl of $type_name$Store {
dojo::model::Model::<$type_name$>::get(world, serialized.span())
}
fn set(self: @$type_name$, world: dojo::world::IWorldDispatcher) {
dojo::model::Model::<$type_name$>::set_model(self, world);
}
fn delete(self: @$type_name$, world: dojo::world::IWorldDispatcher) {
dojo::model::Model::<$type_name$>::delete_model(self, world);
}
$field_accessors$
}
Expand Down Expand Up @@ -406,7 +422,7 @@ pub impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entit
Self::from_values(entity_id, ref values)
}
fn update(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
fn update_entity(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
dojo::world::IWorldDispatcherTrait::set_entity(
world,
dojo::model::Model::<$type_name$>::selector(),
Expand All @@ -416,7 +432,7 @@ pub impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entit
);
}
fn delete(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
fn delete_entity(self: @$type_name$Entity, world: dojo::world::IWorldDispatcher) {
dojo::world::IWorldDispatcherTrait::delete_entity(
world,
dojo::model::Model::<$type_name$>::selector(),
Expand Down Expand Up @@ -507,7 +523,7 @@ pub impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
$type_name$Store::from_values(ref _keys, ref values)
}
fn set(
fn set_model(
self: @$type_name$,
world: dojo::world::IWorldDispatcher
) {
Expand All @@ -520,7 +536,7 @@ pub impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
);
}
fn delete(
fn delete_model(
self: @$type_name$,
world: dojo::world::IWorldDispatcher
) {
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/semantics/test_data/set
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Block(
StatementExpr {
expr: FunctionCall(
ExprFunctionCall {
function: ?6::set,
function: ?6::set_model,
args: [
Value(
Snapshot(
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit e1a349b

Please sign in to comment.