diff --git a/crates/dojo-core/src/base_test.cairo b/crates/dojo-core/src/base_test.cairo index 40b6414822..f42db54184 100644 --- a/crates/dojo-core/src/base_test.cairo +++ b/crates/dojo-core/src/base_test.cairo @@ -178,7 +178,7 @@ mod invalid_model { fn selector(self: @ContractState) -> felt252 { // NOTE: Need to update this value if address changes // Pre-computed address of a contract deployed through the world. - 0x21b19f95ff0f382a069dc7034f95584b300133665ee506789c76ba729e42b66 + 0x562c876783650ced854d9571abba1259cc28b5256fa0994f3d1c4cd2b6c4488 } fn namespace(self: @ContractState) -> ByteArray { diff --git a/crates/dojo-core/src/lib.cairo b/crates/dojo-core/src/lib.cairo index 2aed7f15fe..627cf21739 100644 --- a/crates/dojo-core/src/lib.cairo +++ b/crates/dojo-core/src/lib.cairo @@ -7,6 +7,8 @@ mod database; mod database_test; mod interfaces; mod model; +#[cfg(test)] +mod model_test; mod contract; mod packing; #[cfg(test)] diff --git a/crates/dojo-core/src/model.cairo b/crates/dojo-core/src/model.cairo index d26d43f54a..918576fd94 100644 --- a/crates/dojo-core/src/model.cairo +++ b/crates/dojo-core/src/model.cairo @@ -1,11 +1,32 @@ use dojo::world::IWorldDispatcher; use starknet::SyscallResult; +/// Trait that is implemented at Cairo level for each struct that is a model. +trait ModelValues { + fn values(self: @T) -> Span; + fn from_values(values: Span) -> T; + fn get(world: IWorldDispatcher, id: felt252) -> T; + fn update(self: @T, world: IWorldDispatcher, id: felt252); + fn delete(self: @T, world: IWorldDispatcher, id: felt252); +} + trait Model { fn entity( world: IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout ) -> T; + fn set_entity( + world: IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ); + + fn delete_entity( + world: IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout + ); + + /// Returns the name of the model as it was written in Cairo code. fn name() -> ByteArray; @@ -25,6 +46,7 @@ trait Model { fn name_hash() -> felt252; fn namespace_hash() -> felt252; + fn entity_id(self: @T) -> felt252; fn keys(self: @T) -> Span; fn values(self: @T) -> Span; fn layout() -> dojo::database::introspect::Layout; diff --git a/crates/dojo-core/src/model_test.cairo b/crates/dojo-core/src/model_test.cairo new file mode 100644 index 0000000000..231c4a3890 --- /dev/null +++ b/crates/dojo-core/src/model_test.cairo @@ -0,0 +1,86 @@ +use dojo::test_utils::{spawn_test_world}; +use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; + +// Utils +fn deploy_world() -> IWorldDispatcher { + spawn_test_world("dojo", array![]) +} + +#[derive(Copy, Drop, Serde)] +#[dojo::model] +struct Foo { + #[key] + k1: u8, + #[key] + k2: felt252, + v1: u128, + v2: u32 +} + +#[test] +fn test_values() { + let mvalues = FooValues { v1: 3, v2: 4 }; + let expected_values = array![3, 4].span(); + + let values = FooModelValues::values(@mvalues); + assert!(expected_values == values); +} + +#[test] +fn test_from_values() { + let values = array![3, 4].span(); + + let model_values = FooModelValues::from_values(values); + assert!(model_values.v1 == 3 && model_values.v2 == 4); +} + +#[test] +#[should_panic(expected: "ModelValues `FooValues`: deserialization failed.")] +fn test_from_values_bad_data() { + let values = array![3].span(); + let _ = FooModelValues::from_values(values); +} + +#[test] +fn test_set_and_get_values() { + let world = deploy_world(); + world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); + + let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; + let entity_id = entity.entity_id(); + dojo::model::Model::< + Foo + >::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); + + let mut entity_values = FooModelValues::get(world, entity_id); + assert!(entity.v1 == entity_values.v1 && entity.v2 == entity_values.v2); + + entity_values.v1 = 12; + entity_values.v2 = 18; + + entity_values.update(world, entity_id); + + let read_values = FooModelValues::get(world, entity_id); + assert!(read_values.v1 == entity_values.v1 && read_values.v2 == entity_values.v2); +} + +#[test] +fn test_entity_and_set_entity() { + let world = deploy_world(); + world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); + + let entity = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; + dojo::model::Model::< + Foo + >::set_entity(world, entity.keys(), entity.values(), entity.instance_layout()); + let read_entity = dojo::model::Model::< + Foo + >::entity(world, entity.keys(), entity.instance_layout()); + + assert!( + entity.k1 == read_entity.k1 + && entity.k2 == read_entity.k2 + && entity.v1 == read_entity.v1 + && entity.v2 == read_entity.v2 + ); +} diff --git a/crates/dojo-core/src/resource_metadata.cairo b/crates/dojo-core/src/resource_metadata.cairo index 601f05f391..82b52487ed 100644 --- a/crates/dojo-core/src/resource_metadata.cairo +++ b/crates/dojo-core/src/resource_metadata.cairo @@ -45,6 +45,26 @@ impl ResourceMetadataModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, Self::selector(), keys, values, layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity(world, Self::selector(), keys, layout); + } + + #[inline(always)] fn name() -> ByteArray { "ResourceMetadata" @@ -81,6 +101,11 @@ impl ResourceMetadataModel of dojo::model::Model { dojo::utils::hash(@Self::namespace()) } + #[inline(always)] + fn entity_id(self: @ResourceMetadata) -> felt252 { + poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ResourceMetadata) -> Span { let mut serialized = core::array::ArrayTrait::new(); diff --git a/crates/dojo-core/src/utils.cairo b/crates/dojo-core/src/utils.cairo index 92654ce538..80e318194b 100644 --- a/crates/dojo-core/src/utils.cairo +++ b/crates/dojo-core/src/utils.cairo @@ -4,3 +4,16 @@ fn hash(data: @ByteArray) -> felt252 { Serde::serialize(data, ref serialized); poseidon::poseidon_hash_span(serialized.span()) } + +/// Computes the entity id from the keys. +/// +/// # Arguments +/// +/// * `keys` - The keys of the entity. +/// +/// # Returns +/// +/// The entity id. +fn entity_id_from_keys(keys: Span) -> felt252 { + poseidon::poseidon_hash_span(keys) +} diff --git a/crates/dojo-core/src/world.cairo b/crates/dojo-core/src/world.cairo index 2db09d2f80..d17b0706be 100644 --- a/crates/dojo-core/src/world.cairo +++ b/crates/dojo-core/src/world.cairo @@ -19,6 +19,9 @@ trait IWorld { fn entity( self: @T, model: felt252, keys: Span, layout: dojo::database::introspect::Layout ) -> Span; + fn entity_by_id( + self: @T, model_id: felt252, entity_id: felt252, layout: dojo::database::introspect::Layout + ) -> Span; fn set_entity( ref self: T, model: felt252, @@ -26,9 +29,22 @@ trait IWorld { values: Span, layout: dojo::database::introspect::Layout ); + fn set_entity_by_id( + ref self: T, + model_id: felt252, + entity_id: felt252, + values: Span, + layout: dojo::database::introspect::Layout + ); fn delete_entity( ref self: T, model: felt252, keys: Span, layout: dojo::database::introspect::Layout ); + fn delete_entity_by_id( + ref self: T, + model_id: felt252, + entity_id: felt252, + layout: dojo::database::introspect::Layout + ); fn base(self: @T) -> ClassHash; /// In Dojo, there are 2 levels of authorization: `owner` and `writer`. @@ -108,6 +124,7 @@ mod world { use dojo::world::{IWorldDispatcher, IWorld, IUpgradeableWorld}; use dojo::resource_metadata; use dojo::resource_metadata::ResourceMetadata; + use dojo::utils::entity_id_from_keys; use super::Errors; @@ -135,6 +152,7 @@ mod world { NamespaceRegistered: NamespaceRegistered, ModelRegistered: ModelRegistered, StoreSetRecord: StoreSetRecord, + StoreUpdateRecord: StoreUpdateRecord, StoreDelRecord: StoreDelRecord, WriterUpdated: WriterUpdated, OwnerUpdated: OwnerUpdated, @@ -202,10 +220,17 @@ mod world { values: Span, } + #[derive(Drop, starknet::Event)] + struct StoreUpdateRecord { + model_id: felt252, + entity_id: felt252, + values: Span, + } + #[derive(Drop, starknet::Event)] struct StoreDelRecord { table: felt252, - keys: Span, + entity_id: felt252, } #[derive(Drop, starknet::Event)] @@ -288,14 +313,15 @@ mod world { /// /// `resource_id` - The resource id. fn metadata(self: @ContractState, resource_id: felt252) -> ResourceMetadata { + let mut model = array![resource_id]; + let entity_id = entity_id_from_keys(model.span()); let mut data = self - ._read_model_data( + ._read_model_entity( dojo::model::Model::::selector(), - array![resource_id].span(), + entity_id, Model::::layout() ); - let mut model = array![resource_id]; core::array::serialize_array_helper(data, ref model); let mut model_span = model.span(); @@ -315,11 +341,11 @@ mod world { ); let model = Model::::selector(); - let keys = Model::::keys(@metadata); + let entity_id = Model::::entity_id(@metadata); let values = Model::::values(@metadata); let layout = Model::::layout(); - self._write_model_data(model, keys, values, layout); + self._write_model_entity(model, entity_id, values, layout); EventEmitter::emit( ref self, @@ -741,11 +767,36 @@ mod world { self.can_write_model(model, get_caller_address()), Errors::NO_MODEL_WRITE_ACCESS ); - self._write_model_data(model, keys, values, layout); + let entity_id = entity_id_from_keys(keys); + self._write_model_entity(model, entity_id, values, layout); EventEmitter::emit(ref self, StoreSetRecord { table: model, keys, values }); } - /// Deletes a model from an entity. + /// Sets the model value for an entity. + /// + /// # Arguments + /// + /// * `model` - The selector of the model to be set. + /// * `entity_id` - The id of the entity to set. + /// * `value_names` - The name of model fields which are not a key. + /// * `values` - The value to be set. + /// * `layout` - The memory layout of the entity. + fn set_entity_by_id( + ref self: ContractState, + model_id: felt252, + entity_id: felt252, + values: Span, + layout: dojo::database::introspect::Layout + ) { + assert( + self.can_write_model(model_id, get_caller_address()), Errors::NO_MODEL_WRITE_ACCESS + ); + + self._write_model_entity(model_id, entity_id, values, layout); + EventEmitter::emit(ref self, StoreUpdateRecord { model_id, entity_id, values }); + } + + /// Deletes an existing entity. /// Deleting is setting all the values to 0 in the given layout. /// /// # Arguments @@ -759,13 +810,32 @@ mod world { model: felt252, keys: Span, layout: dojo::database::introspect::Layout + ) { + let entity_id = entity_id_from_keys(keys); + self.delete_entity_by_id(model, entity_id, layout); + } + + /// Deletes an existing entity. + /// Deleting is setting all the values to 0 in the given layout. + /// + /// # Arguments + /// + /// * `model_id` - The selector of the model to be deleted. + /// * `entity_id` - The id to be used to find the entity. + /// * `value_names` - The name of model fields which are not a key. + /// * `layout` - The memory layout of the entity. + fn delete_entity_by_id( + ref self: ContractState, + model_id: felt252, + entity_id: felt252, + layout: dojo::database::introspect::Layout ) { assert( - self.can_write_model(model, get_caller_address()), Errors::NO_MODEL_WRITE_ACCESS + self.can_write_model(model_id, get_caller_address()), Errors::NO_MODEL_WRITE_ACCESS ); - self._delete_model_data(model, keys, layout); - EventEmitter::emit(ref self, StoreDelRecord { table: model, keys }); + self._delete_model_entity(model_id, entity_id, layout); + EventEmitter::emit(ref self, StoreDelRecord { table: model_id, entity_id }); } /// Gets the model value for an entity. Returns a zero initialized @@ -787,7 +857,30 @@ mod world { keys: Span, layout: dojo::database::introspect::Layout ) -> Span { - self._read_model_data(model, keys, layout) + let entity_id = entity_id_from_keys(keys); + self._read_model_entity(model, entity_id, layout) + } + + /// Gets the model value for an entity. Returns a zero initialized + /// model value if the entity has not been set. + /// + /// # Arguments + /// + /// * `model` - The selector of the model to be retrieved. + /// * `entity_id` - The id used to find the entity. + /// * `value_names` - The name of model fields which are not a key. + /// * `layout` - The memory layout of the entity. + /// + /// # Returns + /// + /// * `Span` - The serialized value of the model, zero initialized if not set. + fn entity_by_id( + self: @ContractState, + model_id: felt252, + entity_id: felt252, + layout: dojo::database::introspect::Layout + ) -> Span { + self._read_model_entity(model_id, entity_id, layout) } /// Gets the base contract class hash. @@ -987,78 +1080,76 @@ mod world { || self.is_account_writer(resource_id) } - /// Write a new model record. + /// Write a new entity. /// /// # Arguments - /// * `model` - the model selector - /// * `keys` - the list of model keys to identify the record + /// * `model_id` - the model selector + /// * `entity_id` - the id used to identify the record /// * `values` - the field values of the record /// * `layout` - the model layout - fn _write_model_data( + fn _write_model_entity( ref self: ContractState, - model: felt252, - keys: Span, + model_id: felt252, + entity_id: felt252, values: Span, layout: dojo::database::introspect::Layout ) { - let model_key = poseidon::poseidon_hash_span(keys); let mut offset = 0; match layout { Layout::Fixed(layout) => { - Self::_write_fixed_layout(model, model_key, values, ref offset, layout); + Self::_write_fixed_layout(model_id, entity_id, values, ref offset, layout); }, Layout::Struct(layout) => { - Self::_write_struct_layout(model, model_key, values, ref offset, layout); + Self::_write_struct_layout(model_id, entity_id, values, ref offset, layout); }, _ => { panic!("Unexpected layout type for a model."); } }; } - /// Delete a model record. + /// Delete an entity. /// /// # Arguments /// * `model` - the model selector - /// * `keys` - the list of model keys to identify the record + /// * `entity_id` - the lid used to identify the entity. /// * `layout` - the model layout - fn _delete_model_data( + fn _delete_model_entity( ref self: ContractState, - model: felt252, - keys: Span, + model_id: felt252, + entity_id: felt252, layout: dojo::database::introspect::Layout ) { - let model_key = poseidon::poseidon_hash_span(keys); - match layout { - Layout::Fixed(layout) => { Self::_delete_fixed_layout(model, model_key, layout); }, + Layout::Fixed(layout) => { + Self::_delete_fixed_layout(model_id, entity_id, layout); + }, Layout::Struct(layout) => { - Self::_delete_struct_layout(model, model_key, layout); + Self::_delete_struct_layout(model_id, entity_id, layout); }, _ => { panic!("Unexpected layout type for a model."); } }; } - /// Read a model record. + /// Read an entity. /// /// # Arguments /// * `model` - the model selector - /// * `keys` - the list of model keys to identify the record + /// * `entity_id` - the id used to identify the record /// * `layout` - the model layout - fn _read_model_data( + fn _read_model_entity( self: @ContractState, - model: felt252, - keys: Span, + model_id: felt252, + entity_id: felt252, layout: dojo::database::introspect::Layout ) -> Span { - let model_key = poseidon::poseidon_hash_span(keys); let mut read_data = ArrayTrait::::new(); match layout { Layout::Fixed(layout) => { - Self::_read_fixed_layout(model, model_key, ref read_data, layout); + Self::_read_fixed_layout(model_id, entity_id, ref read_data, layout); }, Layout::Struct(layout) => { - Self::_read_struct_layout(model, model_key, ref read_data, layout); + Self::_read_struct_layout(model_id, entity_id, ref read_data, layout); }, _ => { panic!("Unexpected layout type for a model."); } }; diff --git a/crates/dojo-core/src/world_test.cairo b/crates/dojo-core/src/world_test.cairo index 1958726cfd..dd3f2cb2d3 100644 --- a/crates/dojo-core/src/world_test.cairo +++ b/crates/dojo-core/src/world_test.cairo @@ -22,6 +22,7 @@ use dojo::config::component::Config::{ }; use dojo::model::Model; use dojo::benchmarks::{Character, GasCounterImpl}; +use dojo::utils::entity_id_from_keys; #[derive(Introspect, Copy, Drop, Serde)] enum OneEnum { @@ -957,6 +958,20 @@ fn test_can_call_init() { dojo_init.dojo_init(); } +#[test] +fn test_set_entity_by_id() { + let world = deploy_world(); + world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); + let selector = dojo::model::Model::::selector(); + let entity_id = entity_id_from_keys(array![0x01234].span()); + let values = create_foo(); + let layout = dojo::model::Model::::layout(); + + world.set_entity_by_id(selector, entity_id, values, layout); + let read_values = world.entity_by_id(selector, entity_id, layout); + assert_array(read_values, values); +} + #[test] fn test_set_entity_with_fixed_layout() { let world = deploy_world(); @@ -1126,6 +1141,25 @@ fn test_set_entity_with_struct_generics_enum_layout() { assert_array(read_values, values); } +#[test] +fn test_delete_entity_by_id() { + let world = deploy_world(); + world.register_model(foo::TEST_CLASS_HASH.try_into().unwrap()); + let selector = dojo::model::Model::::selector(); + let entity_id = entity_id_from_keys(get_key_test()); + let values = create_foo(); + let layout = dojo::model::Model::::layout(); + + world.set_entity_by_id(selector, entity_id, values, layout); + + world.delete_entity_by_id(selector, entity_id, layout); + + let read_values = world.entity_by_id(selector, entity_id, layout); + + assert!(read_values.len() == values.len()); + assert_empty_array(read_values); +} + #[test] fn test_delete_entity_with_fixed_layout() { let world = deploy_world(); diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/abis/dojo-world.json b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/abis/dojo-world.json index 0e5e96210e..38a76d87cb 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/abis/dojo-world.json +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/abis/dojo-world.json @@ -301,6 +301,30 @@ ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -325,6 +349,30 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -345,6 +393,26 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -899,6 +967,28 @@ } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -910,8 +1000,8 @@ "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1075,6 +1165,11 @@ "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml index 83a7a20228..bf10b52348 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" -original_class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" +class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" +original_class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/crates/dojo-lang/src/model.rs b/crates/dojo-lang/src/model.rs index 18bdd3ed80..77795a2e5a 100644 --- a/crates/dojo-lang/src/model.rs +++ b/crates/dojo-lang/src/model.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; use cairo_lang_defs::patcher::RewriteNode; use cairo_lang_defs::plugin::PluginDiagnostic; use cairo_lang_diagnostics::Severity; -use cairo_lang_syntax::node::ast::{ArgClause, Expr, ItemStruct, OptionArgListParenthesized}; +use cairo_lang_syntax::node::ast::{ + ArgClause, Expr, ItemStruct, Member as MemberAst, OptionArgListParenthesized, +}; use cairo_lang_syntax::node::db::SyntaxGroup; use cairo_lang_syntax::node::helpers::QueryAttrs; use cairo_lang_syntax::node::{Terminal, TypedStablePtr, TypedSyntaxNode}; @@ -236,19 +238,35 @@ pub fn handle_model_struct( ), }; + let mut members: Vec = vec![]; + let mut members_values: Vec = vec![]; + let mut param_keys: Vec = vec![]; + let mut serialized_keys: Vec = vec![]; + let mut serialized_param_keys: Vec = vec![]; + let mut serialized_values: Vec = vec![]; let elements = struct_ast.members(db).elements(db); - let members: &Vec<_> = &elements - .iter() - .map(|member| Member { - name: member.name(db).text(db).to_string(), - ty: member.type_clause(db).ty(db).as_syntax_node().get_text(db).trim().to_string(), - key: member.has_attr(db, "key"), - }) - .collect::<_>(); - let keys: Vec<_> = members.iter().filter(|m| m.key).collect::<_>(); + elements.iter().for_each(|member_ast| { + let member = Member { + name: member_ast.name(db).text(db).to_string(), + ty: member_ast.type_clause(db).ty(db).as_syntax_node().get_text(db).trim().to_string(), + key: member_ast.has_attr(db, "key"), + }; + + if member.key { + validate_key_member(&member, db, member_ast, &mut diagnostics); + serialized_keys.push(serialize_member_ty(&member, true)); + serialized_param_keys.push(serialize_member_ty(&member, false)); + param_keys.push(format!("{}: {}", member.name, member.ty)); + } else { + serialized_values.push(serialize_member_ty(&member, true)); + members_values.push(RewriteNode::Text(format!("{}: {},\n", member.name, member.ty))); + } + + members.push(member); + }); - if keys.is_empty() { + if serialized_keys.is_empty() { diagnostics.push(PluginDiagnostic { message: "Model must define at least one #[key] attribute".into(), stable_ptr: struct_ast.name(db).stable_ptr().untyped(), @@ -256,7 +274,7 @@ pub fn handle_model_struct( }); } - if keys.len() == members.len() { + if serialized_values.is_empty() { diagnostics.push(PluginDiagnostic { message: "Model must define at least one member that is not a key".into(), stable_ptr: struct_ast.name(db).stable_ptr().untyped(), @@ -264,53 +282,96 @@ pub fn handle_model_struct( }); } - for k in &keys { - if k.ty == "u256" { - diagnostics.push(PluginDiagnostic { - message: "Key is only supported for core types that are 1 felt long once \ - serialized. `u256` is a struct of 2 u128, hence not supported." - .into(), - stable_ptr: struct_ast.name(db).stable_ptr().untyped(), - severity: Severity::Error, - }); - } - } - - let serialize_member = |m: &Member, include_key: bool| { - if m.key && !include_key { - return None; - } - - if m.ty == "felt252" { - return Some(RewriteNode::Text(format!( - "core::array::ArrayTrait::append(ref serialized, *self.{});", - m.name - ))); - } - - Some(RewriteNode::Text(format!( - "core::serde::Serde::serialize(self.{}, ref serialized);", - m.name - ))) - }; - - let serialized_keys: Vec<_> = - keys.iter().filter_map(|m| serialize_member(m, true)).collect::<_>(); - - let serialized_values: Vec<_> = - members.iter().filter_map(|m| serialize_member(m, false)).collect::<_>(); - let name = struct_ast.name(db).text(db); aux_data.models.push(Model { name: name.to_string(), namespace: model_namespace.clone(), - members: members.to_vec(), + members, }); ( RewriteNode::interpolate_patched( " -impl $type_name$Model of dojo::model::Model<$type_name$> { +#[derive(Drop, Serde)] +pub struct $type_name$Values { + $members_values$ +} +#[generate_trait] +impl $type_name$Model of $type_name$Trait { + fn entity_id_from_keys($param_keys$) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + $serialized_param_keys$ + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, $param_keys$) -> $type_name$ { + let mut serialized = core::array::ArrayTrait::new(); + $serialized_param_keys$ + dojo::model::Model::<$type_name$>::entity( + world, + serialized.span(), + dojo::model::Model::<$type_name$>::layout() + ) + } + fn set(self: @$type_name$, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::<$type_name$>::set_entity( + world, + dojo::model::Model::<$type_name$>::keys(self), + dojo::model::Model::<$type_name$>::values(self), + dojo::model::Model::<$type_name$>::layout() + ) + } + fn delete(self: @$type_name$, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::<$type_name$>::delete_entity( + world, + dojo::model::Model::<$type_name$>::keys(self), + dojo::model::Model::<$type_name$>::layout() + ) + } +} +impl $type_name$ModelValues of dojo::model::ModelValues<$type_name$Values> { + fn values(self: @$type_name$Values) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + $serialized_values$ + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> $type_name$Values { + let mut serialized = values; + let entity_values = core::serde::Serde::<$type_name$Values>::deserialize(ref serialized); + if core::option::OptionTrait::<$type_name$Values>::is_none(@entity_values) { + panic!( + \"ModelValues `$type_name$Values`: deserialization failed.\" + ); + } + core::option::OptionTrait::<$type_name$Values>::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> $type_name$Values { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::<$type_name$>::selector(), + id, + dojo::model::Model::<$type_name$>::layout() + ); + Self::from_values(values) + } + fn update(self: @$type_name$Values, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::<$type_name$>::selector(), + id, + self.values(), + dojo::model::Model::<$type_name$>::layout() + ); + } + fn delete(self: @$type_name$Values, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::<$type_name$>::selector(), + id, + dojo::model::Model::<$type_name$>::layout() + ); + } +} +impl $type_name$Impl of dojo::model::Model<$type_name$> { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: \ dojo::database::introspect::Layout) -> $type_name$ { let values = dojo::world::IWorldDispatcherTrait::entity( @@ -339,6 +400,34 @@ impl $type_name$Model of dojo::model::Model<$type_name$> { core::option::OptionTrait::<$type_name$>::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { \"$type_name$\" @@ -379,6 +468,11 @@ impl $type_name$Model of dojo::model::Model<$type_name$> { $model_namespace_hash$ } + #[inline(always)] + fn entity_id(self: @$type_name$) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @$type_name$) -> Span { let mut serialized = core::array::ArrayTrait::new(); @@ -503,8 +597,56 @@ mod $contract_name$ { RewriteNode::Text(model_namespace_hash.to_string()), ), ("model_tag".to_string(), RewriteNode::Text(model_tag.clone())), + ("members_values".to_string(), RewriteNode::new_modified(members_values)), + ("param_keys".to_string(), RewriteNode::Text(param_keys.join(", "))), + ( + "serialized_param_keys".to_string(), + RewriteNode::new_modified(serialized_param_keys), + ), ]), ), diagnostics, ) } + +/// Validates that the key member is valid. +/// # Arguments +/// +/// * member: The member to validate. +/// * diagnostics: The diagnostics to push to, if the member is an invalid key. +fn validate_key_member( + member: &Member, + db: &dyn SyntaxGroup, + member_ast: &MemberAst, + diagnostics: &mut Vec, +) { + if member.ty == "u256" { + diagnostics.push(PluginDiagnostic { + message: "Key is only supported for core types that are 1 felt long once serialized. \ + `u256` is a struct of 2 u128, hence not supported." + .into(), + stable_ptr: member_ast.name(db).stable_ptr().untyped(), + severity: Severity::Error, + }); + } +} + +/// Creates a [`RewriteNode`] for the member type serialization. +/// +/// # Arguments +/// +/// * member: The member to serialize. +fn serialize_member_ty(member: &Member, with_self: bool) -> RewriteNode { + match member.ty.as_str() { + "felt252" => RewriteNode::Text(format!( + "core::array::ArrayTrait::append(ref serialized, {}{});\n", + if with_self { "*self." } else { "" }, + member.name + )), + _ => RewriteNode::Text(format!( + "core::serde::Serde::serialize({}{}, ref serialized);\n", + if with_self { "self." } else { "@" }, + member.name + )), + } +} diff --git a/crates/dojo-lang/src/plugin_test_data/model b/crates/dojo-lang/src/plugin_test_data/model index fd85ef4652..5c5dd11b8f 100644 --- a/crates/dojo-lang/src/plugin_test_data/model +++ b/crates/dojo-lang/src/plugin_test_data/model @@ -395,7 +395,90 @@ dojo::database::introspect::Member { } } -impl BadModelMultipleVersionsModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct BadModelMultipleVersionsValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelMultipleVersionsModel of BadModelMultipleVersionsTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelMultipleVersions { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelMultipleVersions, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelMultipleVersions, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelMultipleVersionsModelValues of dojo::model::ModelValues { + fn values(self: @BadModelMultipleVersionsValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelMultipleVersionsValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelMultipleVersionsValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelMultipleVersionsValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelMultipleVersionsValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelMultipleVersionsValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelMultipleVersionsImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelMultipleVersions { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -422,6 +505,34 @@ impl BadModelMultipleVersionsModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelMultipleVersions" @@ -462,10 +573,16 @@ impl BadModelMultipleVersionsModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelMultipleVersions) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -473,6 +590,7 @@ impl BadModelMultipleVersionsModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -612,7 +730,90 @@ dojo::database::introspect::Member { } } -impl BadModelBadVersionTypeModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct BadModelBadVersionTypeValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelBadVersionTypeModel of BadModelBadVersionTypeTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelBadVersionType { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelBadVersionType, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelBadVersionType, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelBadVersionTypeModelValues of dojo::model::ModelValues { + fn values(self: @BadModelBadVersionTypeValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelBadVersionTypeValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelBadVersionTypeValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelBadVersionTypeValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelBadVersionTypeValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelBadVersionTypeValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelBadVersionTypeImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelBadVersionType { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -639,6 +840,34 @@ impl BadModelBadVersionTypeModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelBadVersionType" @@ -679,10 +908,16 @@ impl BadModelBadVersionTypeModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @BadModelBadVersionType) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelBadVersionType) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -690,6 +925,7 @@ impl BadModelBadVersionTypeModel of dojo::model::Model { fn values(self: @BadModelBadVersionType) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -829,7 +1065,90 @@ dojo::database::introspect::Member { } } -impl BadModelNoVersionValueModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct BadModelNoVersionValueValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelNoVersionValueModel of BadModelNoVersionValueTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNoVersionValue { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelNoVersionValue, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelNoVersionValue, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelNoVersionValueModelValues of dojo::model::ModelValues { + fn values(self: @BadModelNoVersionValueValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelNoVersionValueValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelNoVersionValueValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNoVersionValueValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelNoVersionValueValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelNoVersionValueValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelNoVersionValueImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelNoVersionValue { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -856,6 +1175,34 @@ impl BadModelNoVersionValueModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelNoVersionValue" @@ -896,10 +1243,16 @@ impl BadModelNoVersionValueModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @BadModelNoVersionValue) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelNoVersionValue) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -907,6 +1260,7 @@ impl BadModelNoVersionValueModel of dojo::model::Model { fn values(self: @BadModelNoVersionValue) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -1046,7 +1400,90 @@ dojo::database::introspect::Member { } } -impl BadModelUnexpectedArgWithValueModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct BadModelUnexpectedArgWithValueValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelUnexpectedArgWithValueModel of BadModelUnexpectedArgWithValueTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArgWithValue { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelUnexpectedArgWithValue, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelUnexpectedArgWithValue, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelUnexpectedArgWithValueModelValues of dojo::model::ModelValues { + fn values(self: @BadModelUnexpectedArgWithValueValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelUnexpectedArgWithValueValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelUnexpectedArgWithValueValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArgWithValueValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelUnexpectedArgWithValueValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelUnexpectedArgWithValueValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelUnexpectedArgWithValueImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelUnexpectedArgWithValue { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -1073,6 +1510,34 @@ impl BadModelUnexpectedArgWithValueModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelUnexpectedArgWithValue" @@ -1113,10 +1578,16 @@ impl BadModelUnexpectedArgWithValueModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelUnexpectedArgWithValue) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -1124,6 +1595,7 @@ impl BadModelUnexpectedArgWithValueModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -1263,14 +1735,97 @@ dojo::database::introspect::Member { } } -impl BadModelUnexpectedArgModel of dojo::model::Model { - fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelUnexpectedArg { - let values = dojo::world::IWorldDispatcherTrait::entity( - world, - Self::selector(), - keys, - layout - ); +#[derive(Drop, Serde)] +pub struct BadModelUnexpectedArgValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelUnexpectedArgModel of BadModelUnexpectedArgTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArg { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelUnexpectedArg, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelUnexpectedArg, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelUnexpectedArgModelValues of dojo::model::ModelValues { + fn values(self: @BadModelUnexpectedArgValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelUnexpectedArgValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelUnexpectedArgValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArgValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelUnexpectedArgValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelUnexpectedArgValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelUnexpectedArgImpl of dojo::model::Model { + fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelUnexpectedArg { + let values = dojo::world::IWorldDispatcherTrait::entity( + world, + Self::selector(), + keys, + layout + ); // TODO: Generate method to deserialize from keys / values directly to avoid // serializing to intermediate array. @@ -1290,6 +1845,34 @@ impl BadModelUnexpectedArgModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelUnexpectedArg" @@ -1330,10 +1913,16 @@ impl BadModelUnexpectedArgModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @BadModelUnexpectedArg) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelUnexpectedArg) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -1341,6 +1930,7 @@ impl BadModelUnexpectedArgModel of dojo::model::Model { fn values(self: @BadModelUnexpectedArg) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -1480,7 +2070,90 @@ dojo::database::introspect::Member { } } -impl BadModelNotSupportedVersionModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct BadModelNotSupportedVersionValues { + v: Vec3, + +} +#[generate_trait] +impl BadModelNotSupportedVersionModel of BadModelNotSupportedVersionTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNotSupportedVersion { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @BadModelNotSupportedVersion, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @BadModelNotSupportedVersion, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl BadModelNotSupportedVersionModelValues of dojo::model::ModelValues { + fn values(self: @BadModelNotSupportedVersionValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> BadModelNotSupportedVersionValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `BadModelNotSupportedVersionValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNotSupportedVersionValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @BadModelNotSupportedVersionValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @BadModelNotSupportedVersionValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl BadModelNotSupportedVersionImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> BadModelNotSupportedVersion { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -1507,6 +2180,34 @@ impl BadModelNotSupportedVersionModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "BadModelNotSupportedVersion" @@ -1547,10 +2248,16 @@ impl BadModelNotSupportedVersionModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @BadModelNotSupportedVersion) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -1558,6 +2265,7 @@ impl BadModelNotSupportedVersionModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -1697,7 +2405,90 @@ dojo::database::introspect::Member { } } -impl Modelv0Model of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct Modelv0Values { + v: Vec3, + +} +#[generate_trait] +impl Modelv0Model of Modelv0Trait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> Modelv0 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @Modelv0, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @Modelv0, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl Modelv0ModelValues of dojo::model::ModelValues { + fn values(self: @Modelv0Values) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> Modelv0Values { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `Modelv0Values`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> Modelv0Values { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @Modelv0Values, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @Modelv0Values, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl Modelv0Impl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> Modelv0 { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -1724,6 +2515,34 @@ impl Modelv0Model of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "Modelv0" @@ -1764,10 +2583,16 @@ impl Modelv0Model of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @Modelv0) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @Modelv0) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -1775,6 +2600,7 @@ impl Modelv0Model of dojo::model::Model { fn values(self: @Modelv0) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -1914,7 +2740,90 @@ dojo::database::introspect::Member { } } -impl ModelWithBadNamespaceFormatModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithBadNamespaceFormatValues { + v: Vec3, + +} +#[generate_trait] +impl ModelWithBadNamespaceFormatModel of ModelWithBadNamespaceFormatTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithBadNamespaceFormat { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithBadNamespaceFormat, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithBadNamespaceFormat, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithBadNamespaceFormatModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithBadNamespaceFormatValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithBadNamespaceFormatValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithBadNamespaceFormatValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithBadNamespaceFormatValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithBadNamespaceFormatValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithBadNamespaceFormatValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithBadNamespaceFormatImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithBadNamespaceFormat { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -1941,6 +2850,34 @@ impl ModelWithBadNamespaceFormatModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithBadNamespaceFormat" @@ -1981,10 +2918,16 @@ impl ModelWithBadNamespaceFormatModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithBadNamespaceFormat) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -1992,6 +2935,7 @@ impl ModelWithBadNamespaceFormatModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -2131,9 +3075,92 @@ dojo::database::introspect::Member { } } -impl ModelWithShortStringNamespaceModel of dojo::model::Model { - fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithShortStringNamespace { - let values = dojo::world::IWorldDispatcherTrait::entity( +#[derive(Drop, Serde)] +pub struct ModelWithShortStringNamespaceValues { + v: Vec3, + +} +#[generate_trait] +impl ModelWithShortStringNamespaceModel of ModelWithShortStringNamespaceTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithShortStringNamespace { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithShortStringNamespace, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithShortStringNamespace, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithShortStringNamespaceModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithShortStringNamespaceValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithShortStringNamespaceValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithShortStringNamespaceValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithShortStringNamespaceValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithShortStringNamespaceValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithShortStringNamespaceValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithShortStringNamespaceImpl of dojo::model::Model { + fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithShortStringNamespace { + let values = dojo::world::IWorldDispatcherTrait::entity( world, Self::selector(), keys, @@ -2158,6 +3185,34 @@ impl ModelWithShortStringNamespaceModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithShortStringNamespace" @@ -2198,10 +3253,16 @@ impl ModelWithShortStringNamespaceModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithShortStringNamespace) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -2209,6 +3270,7 @@ impl ModelWithShortStringNamespaceModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -2348,7 +3410,90 @@ dojo::database::introspect::Member { } } -impl ModelWithStringNamespaceModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithStringNamespaceValues { + v: Vec3, + +} +#[generate_trait] +impl ModelWithStringNamespaceModel of ModelWithStringNamespaceTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithStringNamespace { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithStringNamespace, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithStringNamespace, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithStringNamespaceModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithStringNamespaceValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithStringNamespaceValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithStringNamespaceValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithStringNamespaceValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithStringNamespaceValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithStringNamespaceValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithStringNamespaceImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithStringNamespace { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -2375,6 +3520,34 @@ impl ModelWithStringNamespaceModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithStringNamespace" @@ -2415,10 +3588,16 @@ impl ModelWithStringNamespaceModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithStringNamespace) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -2426,6 +3605,7 @@ impl ModelWithStringNamespaceModel of dojo::model::Model Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -2565,7 +3745,90 @@ dojo::database::introspect::Member { } } -impl PositionModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct PositionValues { + v: Vec3, + +} +#[generate_trait] +impl PositionModel of PositionTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> Position { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @Position, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @Position, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl PositionModelValues of dojo::model::ModelValues { + fn values(self: @PositionValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.v, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> PositionValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `PositionValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> PositionValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @PositionValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @PositionValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl PositionImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> Position { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -2592,6 +3855,34 @@ impl PositionModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "Position" @@ -2632,10 +3923,16 @@ impl PositionModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @Position) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @Position) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -2643,6 +3940,7 @@ impl PositionModel of dojo::model::Model { fn values(self: @Position) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.v, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -2782,7 +4080,88 @@ impl RolesIntrospect<> of dojo::database::introspect::Introspect> { } } -impl RolesModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct RolesValues { + role_ids: Array, + +} +#[generate_trait] +impl RolesModel of RolesTrait { + fn entity_id_from_keys() -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, ) -> Roles { + let mut serialized = core::array::ArrayTrait::new(); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @Roles, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @Roles, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl RolesModelValues of dojo::model::ModelValues { + fn values(self: @RolesValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.role_ids, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> RolesValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `RolesValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> RolesValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @RolesValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @RolesValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl RolesImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> Roles { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -2809,6 +4188,34 @@ impl RolesModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "Roles" @@ -2849,6 +4256,11 @@ impl RolesModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @Roles) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @Roles) -> Span { let mut serialized = core::array::ArrayTrait::new(); @@ -2860,6 +4272,7 @@ impl RolesModel of dojo::model::Model { fn values(self: @Roles) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.role_ids, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -2991,15 +4404,96 @@ impl OnlyKeyModelIntrospect<> of dojo::database::introspect::Introspect { - fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> OnlyKeyModel { - let values = dojo::world::IWorldDispatcherTrait::entity( - world, - Self::selector(), - keys, - layout - ); - +#[derive(Drop, Serde)] +pub struct OnlyKeyModelValues { + +} +#[generate_trait] +impl OnlyKeyModelModel of OnlyKeyModelTrait { + fn entity_id_from_keys(id: felt252) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> OnlyKeyModel { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, id); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @OnlyKeyModel, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @OnlyKeyModel, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl OnlyKeyModelModelValues of dojo::model::ModelValues { + fn values(self: @OnlyKeyModelValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> OnlyKeyModelValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `OnlyKeyModelValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> OnlyKeyModelValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @OnlyKeyModelValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @OnlyKeyModelValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl OnlyKeyModelImpl of dojo::model::Model { + fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> OnlyKeyModel { + let values = dojo::world::IWorldDispatcherTrait::entity( + world, + Self::selector(), + keys, + layout + ); + // TODO: Generate method to deserialize from keys / values directly to avoid // serializing to intermediate array. let mut serialized = core::array::ArrayTrait::new(); @@ -3018,6 +4512,34 @@ impl OnlyKeyModelModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "OnlyKeyModel" @@ -3058,10 +4580,16 @@ impl OnlyKeyModelModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @OnlyKeyModel) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @OnlyKeyModel) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.id); + core::array::ArrayTrait::span(@serialized) } @@ -3200,7 +4728,88 @@ impl U256KeyModelIntrospect<> of dojo::database::introspect::Introspect { +#[derive(Drop, Serde)] +pub struct U256KeyModelValues { + +} +#[generate_trait] +impl U256KeyModelModel of U256KeyModelTrait { + fn entity_id_from_keys(id: u256) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@id, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, id: u256) -> U256KeyModel { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@id, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @U256KeyModel, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @U256KeyModel, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl U256KeyModelModelValues of dojo::model::ModelValues { + fn values(self: @U256KeyModelValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> U256KeyModelValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `U256KeyModelValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> U256KeyModelValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @U256KeyModelValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @U256KeyModelValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl U256KeyModelImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> U256KeyModel { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -3227,6 +4836,34 @@ impl U256KeyModelModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "U256KeyModel" @@ -3267,10 +4904,16 @@ impl U256KeyModelModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @U256KeyModel) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @U256KeyModel) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.id, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -3422,7 +5065,92 @@ dojo::database::introspect::Member { } } -impl PlayerModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct PlayerValues { + name: felt252, + +} +#[generate_trait] +impl PlayerModel of PlayerTrait { + fn entity_id_from_keys(game: felt252, player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, game); +core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, game: felt252, player: ContractAddress) -> Player { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, game); +core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @Player, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @Player, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl PlayerModelValues of dojo::model::ModelValues { + fn values(self: @PlayerValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::array::ArrayTrait::append(ref serialized, *self.name); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> PlayerValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `PlayerValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> PlayerValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @PlayerValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @PlayerValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl PlayerImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> Player { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -3449,6 +5177,34 @@ impl PlayerModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "Player" @@ -3489,10 +5245,17 @@ impl PlayerModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @Player) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @Player) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::array::ArrayTrait::append(ref serialized, *self.game);core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::append(ref serialized, *self.game); +core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -3500,6 +5263,7 @@ impl PlayerModel of dojo::model::Model { fn values(self: @Player) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::array::ArrayTrait::append(ref serialized, *self.name); + core::array::ArrayTrait::span(@serialized) } @@ -3653,7 +5417,92 @@ dojo::database::introspect::Member { } } -impl ModelWithSimpleArrayModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithSimpleArrayValues { + x: u16, +y: Array, + +} +#[generate_trait] +impl ModelWithSimpleArrayModel of ModelWithSimpleArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithSimpleArray { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithSimpleArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithSimpleArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithSimpleArrayModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithSimpleArrayValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithSimpleArrayValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithSimpleArrayValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithSimpleArrayValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithSimpleArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithSimpleArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithSimpleArrayImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithSimpleArray { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -3680,6 +5529,34 @@ impl ModelWithSimpleArrayModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithSimpleArray" @@ -3720,17 +5597,25 @@ impl ModelWithSimpleArrayModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @ModelWithSimpleArray) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithSimpleArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } #[inline(always)] fn values(self: @ModelWithSimpleArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::serde::Serde::serialize(self.x, ref serialized);core::serde::Serde::serialize(self.y, ref serialized); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -3880,14 +5765,99 @@ dojo::database::introspect::Member { } } -impl ModelWithByteArrayModel of dojo::model::Model { - fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithByteArray { - let values = dojo::world::IWorldDispatcherTrait::entity( - world, - Self::selector(), - keys, - layout - ); +#[derive(Drop, Serde)] +pub struct ModelWithByteArrayValues { + x: u16, +y: ByteArray, + +} +#[generate_trait] +impl ModelWithByteArrayModel of ModelWithByteArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithByteArray { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithByteArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithByteArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithByteArrayModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithByteArrayValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithByteArrayValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithByteArrayValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithByteArrayValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithByteArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithByteArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithByteArrayImpl of dojo::model::Model { + fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithByteArray { + let values = dojo::world::IWorldDispatcherTrait::entity( + world, + Self::selector(), + keys, + layout + ); // TODO: Generate method to deserialize from keys / values directly to avoid // serializing to intermediate array. @@ -3907,6 +5877,34 @@ impl ModelWithByteArrayModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithByteArray" @@ -3947,17 +5945,25 @@ impl ModelWithByteArrayModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @ModelWithByteArray) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithByteArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } #[inline(always)] fn values(self: @ModelWithByteArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::serde::Serde::serialize(self.x, ref serialized);core::serde::Serde::serialize(self.y, ref serialized); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -4111,7 +6117,92 @@ dojo::database::introspect::Member { } } -impl ModelWithComplexArrayModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithComplexArrayValues { + x: u16, +y: Array, + +} +#[generate_trait] +impl ModelWithComplexArrayModel of ModelWithComplexArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithComplexArray { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithComplexArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithComplexArray, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithComplexArrayModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithComplexArrayValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithComplexArrayValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithComplexArrayValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithComplexArrayValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithComplexArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithComplexArrayValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithComplexArrayImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithComplexArray { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -4138,6 +6229,34 @@ impl ModelWithComplexArrayModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithComplexArray" @@ -4178,17 +6297,25 @@ impl ModelWithComplexArrayModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @ModelWithComplexArray) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithComplexArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } #[inline(always)] fn values(self: @ModelWithComplexArray) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::serde::Serde::serialize(self.x, ref serialized);core::serde::Serde::serialize(self.y, ref serialized); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -4349,7 +6476,92 @@ dojo::database::introspect::Introspect::::ty() } } -impl ModelWithTupleModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithTupleValues { + x: u16, +y: (u8, u16, u32), + +} +#[generate_trait] +impl ModelWithTupleModel of ModelWithTupleTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithTuple { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithTuple, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithTuple, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithTupleModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithTupleValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithTupleValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithTupleValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithTupleValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithTupleValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithTupleValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithTupleImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithTuple { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -4376,6 +6588,34 @@ impl ModelWithTupleModel of dojo::model::Model { core::option::OptionTrait::::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithTuple" @@ -4416,17 +6656,25 @@ impl ModelWithTupleModel of dojo::model::Model { 3437408695301308226171664635441698996501144546809569617702850025816833723775 } + #[inline(always)] + fn entity_id(self: @ModelWithTuple) -> felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithTuple) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } #[inline(always)] fn values(self: @ModelWithTuple) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::serde::Serde::serialize(self.x, ref serialized);core::serde::Serde::serialize(self.y, ref serialized); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -4596,7 +6844,92 @@ dojo::database::introspect::Introspect::::ty() } } -impl ModelWithTupleNoPrimitivesModel of dojo::model::Model { +#[derive(Drop, Serde)] +pub struct ModelWithTupleNoPrimitivesValues { + x: u16, +y: (u8, Vec3, u32), + +} +#[generate_trait] +impl ModelWithTupleNoPrimitivesModel of ModelWithTupleNoPrimitivesTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252 { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + core::poseidon::poseidon_hash_span(serialized.span()) + } + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithTupleNoPrimitives { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(@player, ref serialized); + + dojo::model::Model::::entity( + world, + serialized.span(), + dojo::model::Model::::layout() + ) + } + fn set(self: @ModelWithTupleNoPrimitives, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::set_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::values(self), + dojo::model::Model::::layout() + ) + } + fn delete(self: @ModelWithTupleNoPrimitives, world: dojo::world::IWorldDispatcher) { + dojo::model::Model::::delete_entity( + world, + dojo::model::Model::::keys(self), + dojo::model::Model::::layout() + ) + } +} +impl ModelWithTupleNoPrimitivesModelValues of dojo::model::ModelValues { + fn values(self: @ModelWithTupleNoPrimitivesValues) -> Span { + let mut serialized = core::array::ArrayTrait::new(); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + + core::array::ArrayTrait::span(@serialized) + } + fn from_values(values: Span) -> ModelWithTupleNoPrimitivesValues { + let mut serialized = values; + let entity_values = core::serde::Serde::::deserialize(ref serialized); + if core::option::OptionTrait::::is_none(@entity_values) { + panic!( + "ModelValues `ModelWithTupleNoPrimitivesValues`: deserialization failed." + ); + } + core::option::OptionTrait::::unwrap(entity_values) + } + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithTupleNoPrimitivesValues { + let values = dojo::world::IWorldDispatcherTrait::entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + Self::from_values(values) + } + fn update(self: @ModelWithTupleNoPrimitivesValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::set_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + self.values(), + dojo::model::Model::::layout() + ); + } + fn delete(self: @ModelWithTupleNoPrimitivesValues, world: dojo::world::IWorldDispatcher, id: felt252) { + dojo::world::IWorldDispatcherTrait::delete_entity_by_id( + world, + dojo::model::Model::::selector(), + id, + dojo::model::Model::::layout() + ); + } +} +impl ModelWithTupleNoPrimitivesImpl of dojo::model::Model { fn entity(world: dojo::world::IWorldDispatcher, keys: Span, layout: dojo::database::introspect::Layout) -> ModelWithTupleNoPrimitives { let values = dojo::world::IWorldDispatcherTrait::entity( world, @@ -4623,6 +6956,34 @@ impl ModelWithTupleNoPrimitivesModel of dojo::model::Model::unwrap(entity) } + fn set_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + values: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::set_entity( + world, + Self::selector(), + keys, + values, + layout + ); + } + + fn delete_entity( + world: dojo::world::IWorldDispatcher, + keys: Span, + layout: dojo::database::introspect::Layout + ) { + dojo::world::IWorldDispatcherTrait::delete_entity( + world, + Self::selector(), + keys, + layout + ); + } + #[inline(always)] fn name() -> ByteArray { "ModelWithTupleNoPrimitives" @@ -4663,17 +7024,25 @@ impl ModelWithTupleNoPrimitivesModel of dojo::model::Model felt252 { + core::poseidon::poseidon_hash_span(self.keys()) + } + #[inline(always)] fn keys(self: @ModelWithTupleNoPrimitives) -> Span { let mut serialized = core::array::ArrayTrait::new(); core::serde::Serde::serialize(self.player, ref serialized); + core::array::ArrayTrait::span(@serialized) } #[inline(always)] fn values(self: @ModelWithTupleNoPrimitives) -> Span { let mut serialized = core::array::ArrayTrait::new(); - core::serde::Serde::serialize(self.x, ref serialized);core::serde::Serde::serialize(self.y, ref serialized); + core::serde::Serde::serialize(self.x, ref serialized); +core::serde::Serde::serialize(self.y, ref serialized); + core::array::ArrayTrait::span(@serialized) } @@ -4771,6 +7140,356 @@ mod model_with_tuple_no_primitives { } } } +impl BadModelMultipleVersionsValuesDrop of core::traits::Drop::; +impl BadModelMultipleVersionsValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelMultipleVersionsValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelMultipleVersionsValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelMultipleVersionsTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelMultipleVersions; + fn set(self: @BadModelMultipleVersions, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelMultipleVersions, world: dojo::world::IWorldDispatcher); +} +impl BadModelBadVersionTypeValuesDrop of core::traits::Drop::; +impl BadModelBadVersionTypeValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelBadVersionTypeValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelBadVersionTypeValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelBadVersionTypeTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelBadVersionType; + fn set(self: @BadModelBadVersionType, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelBadVersionType, world: dojo::world::IWorldDispatcher); +} +impl BadModelNoVersionValueValuesDrop of core::traits::Drop::; +impl BadModelNoVersionValueValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelNoVersionValueValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelNoVersionValueValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelNoVersionValueTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNoVersionValue; + fn set(self: @BadModelNoVersionValue, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelNoVersionValue, world: dojo::world::IWorldDispatcher); +} +impl BadModelUnexpectedArgWithValueValuesDrop of core::traits::Drop::; +impl BadModelUnexpectedArgWithValueValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelUnexpectedArgWithValueValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelUnexpectedArgWithValueValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelUnexpectedArgWithValueTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArgWithValue; + fn set(self: @BadModelUnexpectedArgWithValue, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelUnexpectedArgWithValue, world: dojo::world::IWorldDispatcher); +} +impl BadModelUnexpectedArgValuesDrop of core::traits::Drop::; +impl BadModelUnexpectedArgValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelUnexpectedArgValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelUnexpectedArgValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelUnexpectedArgTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelUnexpectedArg; + fn set(self: @BadModelUnexpectedArg, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelUnexpectedArg, world: dojo::world::IWorldDispatcher); +} +impl BadModelNotSupportedVersionValuesDrop of core::traits::Drop::; +impl BadModelNotSupportedVersionValuesSerde of core::serde::Serde:: { + fn serialize(self: @BadModelNotSupportedVersionValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(BadModelNotSupportedVersionValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait BadModelNotSupportedVersionTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> BadModelNotSupportedVersion; + fn set(self: @BadModelNotSupportedVersion, world: dojo::world::IWorldDispatcher); + fn delete(self: @BadModelNotSupportedVersion, world: dojo::world::IWorldDispatcher); +} +impl Modelv0ValuesDrop of core::traits::Drop::; +impl Modelv0ValuesSerde of core::serde::Serde:: { + fn serialize(self: @Modelv0Values, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(Modelv0Values { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait Modelv0Trait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> Modelv0; + fn set(self: @Modelv0, world: dojo::world::IWorldDispatcher); + fn delete(self: @Modelv0, world: dojo::world::IWorldDispatcher); +} +impl ModelWithBadNamespaceFormatValuesDrop of core::traits::Drop::; +impl ModelWithBadNamespaceFormatValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithBadNamespaceFormatValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithBadNamespaceFormatValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithBadNamespaceFormatTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithBadNamespaceFormat; + fn set(self: @ModelWithBadNamespaceFormat, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithBadNamespaceFormat, world: dojo::world::IWorldDispatcher); +} +impl ModelWithShortStringNamespaceValuesDrop of core::traits::Drop::; +impl ModelWithShortStringNamespaceValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithShortStringNamespaceValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithShortStringNamespaceValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithShortStringNamespaceTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithShortStringNamespace; + fn set(self: @ModelWithShortStringNamespace, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithShortStringNamespace, world: dojo::world::IWorldDispatcher); +} +impl ModelWithStringNamespaceValuesDrop of core::traits::Drop::; +impl ModelWithStringNamespaceValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithStringNamespaceValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithStringNamespaceValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithStringNamespaceTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> ModelWithStringNamespace; + fn set(self: @ModelWithStringNamespace, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithStringNamespace, world: dojo::world::IWorldDispatcher); +} +impl PositionValuesDrop of core::traits::Drop::; +impl PositionValuesSerde of core::serde::Serde:: { + fn serialize(self: @PositionValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.v, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(PositionValues { + v: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait PositionTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> Position; + fn set(self: @Position, world: dojo::world::IWorldDispatcher); + fn delete(self: @Position, world: dojo::world::IWorldDispatcher); +} +impl RolesValuesDrop of core::traits::Drop::; +impl RolesValuesSerde of core::serde::Serde:: { + fn serialize(self: @RolesValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.role_ids, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(RolesValues { + role_ids: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait RolesTrait { + fn entity_id_from_keys() -> felt252; + fn get(world: dojo::world::IWorldDispatcher, ) -> Roles; + fn set(self: @Roles, world: dojo::world::IWorldDispatcher); + fn delete(self: @Roles, world: dojo::world::IWorldDispatcher); +} +impl OnlyKeyModelValuesDrop of core::traits::Drop::; +impl OnlyKeyModelValuesSerde of core::serde::Serde:: { + fn serialize(self: @OnlyKeyModelValues, ref output: core::array::Array) { + + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(OnlyKeyModelValues { + + }) + } +} +trait OnlyKeyModelTrait { + fn entity_id_from_keys(id: felt252) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: felt252) -> OnlyKeyModel; + fn set(self: @OnlyKeyModel, world: dojo::world::IWorldDispatcher); + fn delete(self: @OnlyKeyModel, world: dojo::world::IWorldDispatcher); +} +impl U256KeyModelValuesDrop of core::traits::Drop::; +impl U256KeyModelValuesSerde of core::serde::Serde:: { + fn serialize(self: @U256KeyModelValues, ref output: core::array::Array) { + + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(U256KeyModelValues { + + }) + } +} +trait U256KeyModelTrait { + fn entity_id_from_keys(id: u256) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, id: u256) -> U256KeyModel; + fn set(self: @U256KeyModel, world: dojo::world::IWorldDispatcher); + fn delete(self: @U256KeyModel, world: dojo::world::IWorldDispatcher); +} +impl PlayerValuesDrop of core::traits::Drop::; +impl PlayerValuesSerde of core::serde::Serde:: { + fn serialize(self: @PlayerValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.name, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(PlayerValues { + name: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait PlayerTrait { + fn entity_id_from_keys(game: felt252, player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, game: felt252, player: ContractAddress) -> Player; + fn set(self: @Player, world: dojo::world::IWorldDispatcher); + fn delete(self: @Player, world: dojo::world::IWorldDispatcher); +} +impl ModelWithSimpleArrayValuesDrop of core::traits::Drop::; +impl ModelWithSimpleArrayValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithSimpleArrayValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.x, ref output); + core::serde::Serde::serialize(self.y, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithSimpleArrayValues { + x: core::serde::Serde::deserialize(ref serialized)?, + y: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithSimpleArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithSimpleArray; + fn set(self: @ModelWithSimpleArray, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithSimpleArray, world: dojo::world::IWorldDispatcher); +} +impl ModelWithByteArrayValuesDrop of core::traits::Drop::; +impl ModelWithByteArrayValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithByteArrayValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.x, ref output); + core::serde::Serde::serialize(self.y, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithByteArrayValues { + x: core::serde::Serde::deserialize(ref serialized)?, + y: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithByteArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithByteArray; + fn set(self: @ModelWithByteArray, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithByteArray, world: dojo::world::IWorldDispatcher); +} +impl ModelWithComplexArrayValuesDrop of core::traits::Drop::; +impl ModelWithComplexArrayValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithComplexArrayValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.x, ref output); + core::serde::Serde::serialize(self.y, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithComplexArrayValues { + x: core::serde::Serde::deserialize(ref serialized)?, + y: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithComplexArrayTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithComplexArray; + fn set(self: @ModelWithComplexArray, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithComplexArray, world: dojo::world::IWorldDispatcher); +} +impl ModelWithTupleValuesDrop of core::traits::Drop::; +impl ModelWithTupleValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithTupleValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.x, ref output); + core::serde::Serde::serialize(self.y, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithTupleValues { + x: core::serde::Serde::deserialize(ref serialized)?, + y: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithTupleTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithTuple; + fn set(self: @ModelWithTuple, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithTuple, world: dojo::world::IWorldDispatcher); +} +impl ModelWithTupleNoPrimitivesValuesDrop of core::traits::Drop::; +impl ModelWithTupleNoPrimitivesValuesSerde of core::serde::Serde:: { + fn serialize(self: @ModelWithTupleNoPrimitivesValues, ref output: core::array::Array) { + core::serde::Serde::serialize(self.x, ref output); + core::serde::Serde::serialize(self.y, ref output) + } + fn deserialize(ref serialized: core::array::Span) -> core::option::Option { + core::option::Option::Some(ModelWithTupleNoPrimitivesValues { + x: core::serde::Serde::deserialize(ref serialized)?, + y: core::serde::Serde::deserialize(ref serialized)?, + }) + } +} +trait ModelWithTupleNoPrimitivesTrait { + fn entity_id_from_keys(player: ContractAddress) -> felt252; + fn get(world: dojo::world::IWorldDispatcher, player: ContractAddress) -> ModelWithTupleNoPrimitives; + fn set(self: @ModelWithTupleNoPrimitives, world: dojo::world::IWorldDispatcher); + fn delete(self: @ModelWithTupleNoPrimitives, world: dojo::world::IWorldDispatcher); +} //! > expected_diagnostics error: A Dojo model must have zero or one dojo::model attribute. @@ -4818,12 +7537,12 @@ error: Model must define at least one member that is not a key struct OnlyKeyModel { ^**********^ -error: Model must define at least one member that is not a key - --> test_src/lib.cairo:98:8 -struct U256KeyModel { - ^**********^ - error: Key is only supported for core types that are 1 felt long once serialized. `u256` is a struct of 2 u128, hence not supported. + --> test_src/lib.cairo:100:5 + id: u256 + ^^ + +error: Model must define at least one member that is not a key --> test_src/lib.cairo:98:8 struct U256KeyModel { ^**********^ diff --git a/crates/dojo-world/src/contracts/abi/world.rs b/crates/dojo-world/src/contracts/abi/world.rs index f681f1ab97..62f1b0b137 100644 --- a/crates/dojo-world/src/contracts/abi/world.rs +++ b/crates/dojo-world/src/contracts/abi/world.rs @@ -307,6 +307,30 @@ abigen!( ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -331,6 +355,30 @@ abigen!( "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -351,6 +399,26 @@ abigen!( "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -905,6 +973,28 @@ abigen!( } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -916,8 +1006,8 @@ abigen!( "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1081,6 +1171,11 @@ abigen!( "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", diff --git a/crates/sozo/ops/src/events.rs b/crates/sozo/ops/src/events.rs index 4092613eb0..8c4703021c 100644 --- a/crates/sozo/ops/src/events.rs +++ b/crates/sozo/ops/src/events.rs @@ -279,7 +279,7 @@ mod tests { let result = extract_events(&manifest, &project_dir, &target_dir).unwrap(); // we are just collecting all events from manifest file so just verifying count should work - assert_eq!(result.len(), 16); + assert_eq!(result.len(), 18); } #[test] diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index 408f29ab3c..b27840420f 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -29,8 +29,7 @@ rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x504b804eeac62e68d12dc030e56b8f62cb047950c346e60a974da02795f6aba" - +world_address = "0x6997a9f670ca240d7dca99fa26fcb702f5922b0c26d454a04db62876f5c2661" # `release` profile # # for now configurations in `tool` are not merged recursively so to override diff --git a/examples/spawn-and-move/manifests/dev/base/abis/dojo-world.json b/examples/spawn-and-move/manifests/dev/base/abis/dojo-world.json index 0e5e96210e..38a76d87cb 100644 --- a/examples/spawn-and-move/manifests/dev/base/abis/dojo-world.json +++ b/examples/spawn-and-move/manifests/dev/base/abis/dojo-world.json @@ -301,6 +301,30 @@ ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -325,6 +349,30 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -345,6 +393,26 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -899,6 +967,28 @@ } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -910,8 +1000,8 @@ "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1075,6 +1165,11 @@ "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", diff --git a/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml b/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml index b3fc65dfe2..8a0bda9f6a 100644 --- a/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml +++ b/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml @@ -1,6 +1,6 @@ kind = "DojoContract" -class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" -original_class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" +class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" +original_class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" base_class_hash = "0x0" abi = "manifests/dev/base/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] diff --git a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml index 83a7a20228..bf10b52348 100644 --- a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" -original_class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" +class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" +original_class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/examples/spawn-and-move/manifests/dev/deployment/abis/dojo-world.json b/examples/spawn-and-move/manifests/dev/deployment/abis/dojo-world.json index 0e5e96210e..38a76d87cb 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/abis/dojo-world.json +++ b/examples/spawn-and-move/manifests/dev/deployment/abis/dojo-world.json @@ -301,6 +301,30 @@ ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -325,6 +349,30 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -345,6 +393,26 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -899,6 +967,28 @@ } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -910,8 +1000,8 @@ "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1075,6 +1165,11 @@ "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.json b/examples/spawn-and-move/manifests/dev/deployment/manifest.json index 99501691fc..177e19e53c 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.json +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.json @@ -1,8 +1,8 @@ { "world": { "kind": "WorldContract", - "class_hash": "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51", - "original_class_hash": "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51", + "class_hash": "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61", + "original_class_hash": "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61", "abi": [ { "type": "impl", @@ -306,6 +306,30 @@ ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -330,6 +354,30 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -350,6 +398,26 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -904,6 +972,28 @@ } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -915,8 +1005,8 @@ "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1080,6 +1170,11 @@ "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", @@ -1108,8 +1203,8 @@ ] } ], - "address": "0x504b804eeac62e68d12dc030e56b8f62cb047950c346e60a974da02795f6aba", - "transaction_hash": "0x65168705b42b38a0b9e290dfb079e023fa1960febb9fd7069fce5e369e3b56f", + "address": "0x6997a9f670ca240d7dca99fa26fcb702f5922b0c26d454a04db62876f5c2661", + "transaction_hash": "0x4d1e6ab2b50ba6629908bee409050f32363422bf918e4f1cc9913eeaf688cd0", "block_number": 3, "seed": "dojo_examples", "metadata": { @@ -1129,9 +1224,9 @@ "contracts": [ { "kind": "DojoContract", - "address": "0x2e85647f570d2a4abdffdd668823702548d20d1d6aaef74b6a4eb9670a32798", - "class_hash": "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d", - "original_class_hash": "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d", + "address": "0x370a8494164d49b607864fb948820db4f21a91dc53eac3e2a58090ceb276c36", + "class_hash": "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2", + "original_class_hash": "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", "abi": [ { @@ -1524,7 +1619,7 @@ }, { "kind": "DojoContract", - "address": "0x6a06eb156f956f1691bce4a4573c97b1a5f08d3bfa65fc25c4ed4b55d3e61a1", + "address": "0x4a90d939c2aaa49a8761e9b4b88e01cad226b6b2c4fd2c5371c7ef1b71ddef4", "class_hash": "0x97cd4fb3acfe9e6a024589ea34db2fc587586d699ef3732ce627e1771158ef", "original_class_hash": "0x97cd4fb3acfe9e6a024589ea34db2fc587586d699ef3732ce627e1771158ef", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -1743,7 +1838,7 @@ }, { "kind": "DojoContract", - "address": "0x6359537114e0a62c63f52f2fb3c0e6241c9a2fcbd9dc9df221c867662aa31e9", + "address": "0x58eea790580fc315042c6486f68b9d15298d6f01771e3991d42827fe6fd6744", "class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "original_class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml index 6c9080ead0..46b9be732f 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml @@ -1,10 +1,10 @@ [world] kind = "WorldContract" -class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" -original_class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" +class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" +original_class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" abi = "manifests/dev/deployment/abis/dojo-world.json" -address = "0x504b804eeac62e68d12dc030e56b8f62cb047950c346e60a974da02795f6aba" -transaction_hash = "0x65168705b42b38a0b9e290dfb079e023fa1960febb9fd7069fce5e369e3b56f" +address = "0x6997a9f670ca240d7dca99fa26fcb702f5922b0c26d454a04db62876f5c2661" +transaction_hash = "0x4d1e6ab2b50ba6629908bee409050f32363422bf918e4f1cc9913eeaf688cd0" block_number = 3 seed = "dojo_examples" manifest_name = "dojo-world" @@ -23,9 +23,9 @@ manifest_name = "dojo-base" [[contracts]] kind = "DojoContract" -address = "0x2e85647f570d2a4abdffdd668823702548d20d1d6aaef74b6a4eb9670a32798" -class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" -original_class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" +address = "0x370a8494164d49b607864fb948820db4f21a91dc53eac3e2a58090ceb276c36" +class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" +original_class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" abi = "manifests/dev/deployment/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] @@ -40,7 +40,7 @@ manifest_name = "dojo_examples-actions-40b6994c" [[contracts]] kind = "DojoContract" -address = "0x6a06eb156f956f1691bce4a4573c97b1a5f08d3bfa65fc25c4ed4b55d3e61a1" +address = "0x4a90d939c2aaa49a8761e9b4b88e01cad226b6b2c4fd2c5371c7ef1b71ddef4" class_hash = "0x97cd4fb3acfe9e6a024589ea34db2fc587586d699ef3732ce627e1771158ef" original_class_hash = "0x97cd4fb3acfe9e6a024589ea34db2fc587586d699ef3732ce627e1771158ef" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -54,7 +54,7 @@ manifest_name = "dojo_examples-mock_token-31599eb2" [[contracts]] kind = "DojoContract" -address = "0x6359537114e0a62c63f52f2fb3c0e6241c9a2fcbd9dc9df221c867662aa31e9" +address = "0x58eea790580fc315042c6486f68b9d15298d6f01771e3991d42827fe6fd6744" class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" original_class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" diff --git a/examples/spawn-and-move/manifests/release/base/abis/dojo-world.json b/examples/spawn-and-move/manifests/release/base/abis/dojo-world.json index 0e5e96210e..38a76d87cb 100644 --- a/examples/spawn-and-move/manifests/release/base/abis/dojo-world.json +++ b/examples/spawn-and-move/manifests/release/base/abis/dojo-world.json @@ -301,6 +301,30 @@ ], "state_mutability": "view" }, + { + "type": "function", + "name": "entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [ + { + "type": "core::array::Span::" + } + ], + "state_mutability": "view" + }, { "type": "function", "name": "set_entity", @@ -325,6 +349,30 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "set_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "values", + "type": "core::array::Span::" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "delete_entity", @@ -345,6 +393,26 @@ "outputs": [], "state_mutability": "external" }, + { + "type": "function", + "name": "delete_entity_by_id", + "inputs": [ + { + "name": "model_id", + "type": "core::felt252" + }, + { + "name": "entity_id", + "type": "core::felt252" + }, + { + "name": "layout", + "type": "dojo::database::introspect::Layout" + } + ], + "outputs": [], + "state_mutability": "external" + }, { "type": "function", "name": "base", @@ -899,6 +967,28 @@ } ] }, + { + "type": "event", + "name": "dojo::world::world::StoreUpdateRecord", + "kind": "struct", + "members": [ + { + "name": "model_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "entity_id", + "type": "core::felt252", + "kind": "data" + }, + { + "name": "values", + "type": "core::array::Span::", + "kind": "data" + } + ] + }, { "type": "event", "name": "dojo::world::world::StoreDelRecord", @@ -910,8 +1000,8 @@ "kind": "data" }, { - "name": "keys", - "type": "core::array::Span::", + "name": "entity_id", + "type": "core::felt252", "kind": "data" } ] @@ -1075,6 +1165,11 @@ "type": "dojo::world::world::StoreSetRecord", "kind": "nested" }, + { + "name": "StoreUpdateRecord", + "type": "dojo::world::world::StoreUpdateRecord", + "kind": "nested" + }, { "name": "StoreDelRecord", "type": "dojo::world::world::StoreDelRecord", diff --git a/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml b/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml index aa9b1571cd..b357f9172c 100644 --- a/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml +++ b/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml @@ -1,6 +1,6 @@ kind = "DojoContract" -class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" -original_class_hash = "0x647694c15fff8476c82ea146d0dbdbbb17d3425d5ba05a7320e4322f916df1d" +class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" +original_class_hash = "0x7b605612a7c49a92dd752a50f3379f63d5ac89c8b4ac4f374e28f2f042852a2" base_class_hash = "0x0" abi = "manifests/release/base/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] diff --git a/examples/spawn-and-move/manifests/release/base/dojo-world.toml b/examples/spawn-and-move/manifests/release/base/dojo-world.toml index 97f4f8c522..7a9495cd20 100644 --- a/examples/spawn-and-move/manifests/release/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/release/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" -original_class_hash = "0x210dd8e484e5555dc74a4a600b17878fc108911719b04139309acc874160c51" +class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" +original_class_hash = "0x421b7e4f94c441f6249973a26ef528381689b3a35602b7b46e1439b6f81b61" abi = "manifests/release/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/examples/spawn-and-move/src/actions.cairo b/examples/spawn-and-move/src/actions.cairo index a0d7753e0f..e7f5238b66 100644 --- a/examples/spawn-and-move/src/actions.cairo +++ b/examples/spawn-and-move/src/actions.cairo @@ -23,7 +23,8 @@ mod actions { use starknet::{ContractAddress, get_caller_address}; use dojo_examples::models::{ - Position, Moves, Direction, Vec2, PlayerConfig, PlayerItem, ServerProfile + Position, Moves, Direction, Vec2, PlayerConfig, PlayerItem, ServerProfile, PositionModel, + MovesModel, MovesValues, MovesModelValues, PlayerConfigModel }; use dojo_examples::utils::next_position; @@ -73,11 +74,28 @@ mod actions { fn move(ref world: IWorldDispatcher, direction: Direction) { let player = get_caller_address(); - let (mut position, mut moves) = get!(world, player, (Position, Moves)); + + // instead of using the `get!` macro, you can directly use + // the Model::get method + let mut position = PositionModel::get(world, player); + + // you can also get entity values by entity ID with the `ModelValues` trait. + // Note that it returns a `Values` struct which contains + // model values only. + let move_id = MovesModel::entity_id_from_keys(player); + let mut moves = MovesModelValues::get(world, move_id); + moves.remaining -= 1; moves.last_direction = direction; let next = next_position(position, direction); - set!(world, (moves, next)); + + // instead of using the `set!` macro, you can directly use + // the Model::set method + next.set(world); + + // you can also update entity values by entity ID with the `ModelValues` trait. + moves.update(world, move_id); + emit!(world, (Moved { player, direction })); } @@ -96,9 +114,11 @@ mod actions { fn reset_player_config(ref world: IWorldDispatcher) { let player = get_caller_address(); - let (position, moves, config) = get!(world, player, (Position, Moves, PlayerConfig)); + let (position, moves) = get!(world, player, (Position, Moves)); + let config = PlayerConfigModel::get(world, player); - delete!(world, (position, moves, config)); + delete!(world, (position, moves)); + config.delete(world); let (position, moves, config) = get!(world, player, (Position, Moves, PlayerConfig));