Skip to content

Commit

Permalink
move to edition 2023_11
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed Jul 16, 2024
1 parent 9485d3f commit 1a55dd6
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 172 deletions.
2 changes: 1 addition & 1 deletion crates/dojo-core/src/model_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn deploy_world() -> IWorldDispatcher {
spawn_test_world("dojo", array![])
}

#[derive(Copy, Drop, Serde)]
#[derive(Copy, Drop, Serde, Debug)]
#[dojo::model]
struct Foo {
#[key]
Expand Down
30 changes: 17 additions & 13 deletions crates/dojo-core/src/resource_metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ struct ResourceMetadata {
metadata_uri: ByteArray,
}

#[generate_trait]
impl ResourceMetadataImpl of ResourceMetadataTrait {
fn from_values(resource_id: felt252, ref values: Span<felt252>) -> ResourceMetadata {
let metadata_uri = core::serde::Serde::<ByteArray>::deserialize(ref values);
if metadata_uri.is_none() {
panic!("Model `ResourceMetadata`: metadata_uri deserialization failed.");
}

ResourceMetadata { resource_id, metadata_uri: metadata_uri.unwrap() }
}
}

impl ResourceMetadataModel of dojo::model::Model<ResourceMetadata> {
fn get(world: dojo::world::IWorldDispatcher, keys: Span<felt252>) -> ResourceMetadata {
let values = world.entity(Self::selector(), ModelIndex::Keys(keys), Self::layout());
let mut serialized = core::array::ArrayTrait::new();
core::array::serialize_array_helper(keys, ref serialized);
core::array::serialize_array_helper(values, ref serialized);
let mut serialized = core::array::ArrayTrait::span(@serialized);
let entity = core::serde::Serde::<ResourceMetadata>::deserialize(ref serialized);

if core::option::OptionTrait::<ResourceMetadata>::is_none(@entity) {
panic!(
"Model `ResourceMetadata`: deserialization failed. Ensure the length of the keys tuple is matching the number of #[key] fields in the model struct."
);
}
if keys.len() != 1 {
panic!("Model `ResourceMetadata`: bad keys length.");
};

core::option::OptionTrait::<ResourceMetadata>::unwrap(entity)
let mut values = world.entity(Self::selector(), ModelIndex::Keys(keys), Self::layout());
ResourceMetadataTrait::from_values(*keys.at(0), ref values)
}

fn set(self: @ResourceMetadata, world: dojo::world::IWorldDispatcher,) {
Expand Down
32 changes: 13 additions & 19 deletions crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ mod world {
};
use dojo::world::{IWorldDispatcher, IWorld, IUpgradeableWorld};
use dojo::resource_metadata;
use dojo::resource_metadata::ResourceMetadata;
use dojo::resource_metadata::{ResourceMetadata, ResourceMetadataTrait};
use dojo::utils::entity_id_from_keys;

use super::{Errors, ModelIndex};
Expand Down Expand Up @@ -317,20 +317,13 @@ 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_entity(
let mut values = self
.entity(
dojo::model::Model::<ResourceMetadata>::selector(),
entity_id,
Model::<ResourceMetadata>::layout()
ModelIndex::Keys(array![resource_id].span()),
dojo::model::Model::<ResourceMetadata>::layout()
);

core::array::serialize_array_helper(data, ref model);

let mut model_span = model.span();

Serde::<ResourceMetadata>::deserialize(ref model_span).expect(Errors::METADATA_DESER)
ResourceMetadataTrait::from_values(resource_id, ref values)
}

/// Sets the metadata of the resource.
Expand All @@ -344,12 +337,13 @@ mod world {
Errors::NO_WRITE_ACCESS
);

let model = Model::<ResourceMetadata>::selector();
let entity_id = Model::<ResourceMetadata>::entity_id(@metadata);
let values = Model::<ResourceMetadata>::values(@metadata);
let layout = Model::<ResourceMetadata>::layout();

self._write_model_entity(model, entity_id, values, layout);
self
._write_model_entity(
metadata.instance_selector(),
metadata.entity_id(),
metadata.values(),
metadata.instance_layout()
);

EventEmitter::emit(
ref self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "compiler_cairo"
version = "0.1.0"
edition = "2023_10"
edition = "2023_11"
cairo-version = "2.6.3"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557"
original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557"
class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb"
original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
71 changes: 33 additions & 38 deletions crates/dojo-lang/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ pub fn handle_model_struct(
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_values
.push(RewriteNode::Text(format!("pub {}: {},\n", member.name, member.ty)));
}

members.push(member);
Expand Down Expand Up @@ -325,7 +326,7 @@ pub struct $type_name$Entity {
}
#[generate_trait]
impl $type_name$EntityImpl of $type_name$EntityTrait {
pub impl $type_name$EntityImpl of $type_name$EntityTrait {
fn get(world: dojo::world::IWorldDispatcher, entity_id: felt252) -> $type_name$Entity {
$type_name$ModelEntityImpl::get(world, entity_id)
}
Expand All @@ -334,13 +335,31 @@ impl $type_name$EntityImpl of $type_name$EntityTrait {
}
#[generate_trait]
impl $type_name$Impl of $type_name$Trait {
pub impl $type_name$Impl 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 from_values(ref keys: Span<felt252>, ref values: Span<felt252>) -> $type_name$ {
let mut serialized = core::array::ArrayTrait::new();
serialized.append_span(keys);
serialized.append_span(values);
let mut serialized = core::array::ArrayTrait::span(@serialized);
let entity = core::serde::Serde::<$type_name$>::deserialize(ref serialized);
if core::option::OptionTrait::<$type_name$>::is_none(@entity) {
panic!(
\"Model `$type_name$`: deserialization failed. Ensure the length of the keys tuple \
is matching the number of #[key] fields in the model struct.\"
);
}
core::option::OptionTrait::<$type_name$>::unwrap(entity)
}
fn get(world: dojo::world::IWorldDispatcher, $param_keys$) -> $type_name$ {
let mut serialized = core::array::ArrayTrait::new();
$serialized_param_keys$
Expand All @@ -351,7 +370,7 @@ impl $type_name$Impl of $type_name$Trait {
$field_accessors$
}
impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> {
pub impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> {
fn id(self: @$type_name$Entity) -> felt252 {
*self.__id
}
Expand Down Expand Up @@ -451,32 +470,17 @@ impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> {
}
}
impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
pub impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
fn get(world: dojo::world::IWorldDispatcher, keys: Span<felt252>) -> $type_name$ {
let values = dojo::world::IWorldDispatcherTrait::entity(
let mut values = dojo::world::IWorldDispatcherTrait::entity(
world,
Self::selector(),
dojo::world::ModelIndex::Keys(keys),
Self::layout()
);
let mut _keys = keys;
// TODO: Generate method to deserialize from keys / values directly to avoid
// serializing to intermediate array.
let mut serialized = core::array::ArrayTrait::new();
core::array::serialize_array_helper(keys, ref serialized);
core::array::serialize_array_helper(values, ref serialized);
let mut serialized = core::array::ArrayTrait::span(@serialized);
let entity = core::serde::Serde::<$type_name$>::deserialize(ref serialized);
if core::option::OptionTrait::<$type_name$>::is_none(@entity) {
panic!(
\"Model `$type_name$`: deserialization failed. Ensure the length of the keys tuple \
is matching the number of #[key] fields in the model struct.\"
);
}
core::option::OptionTrait::<$type_name$>::unwrap(entity)
$type_name$Trait::from_values(ref _keys, ref values)
}
fn set(
Expand Down Expand Up @@ -631,12 +635,12 @@ impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
}
#[starknet::interface]
trait I$contract_name$<T> {
pub trait I$contract_name$<T> {
fn ensure_abi(self: @T, model: $type_name$);
}
#[starknet::contract]
mod $contract_name$ {
pub mod $contract_name$ {
use super::$type_name$;
use super::I$contract_name$;
Expand Down Expand Up @@ -795,17 +799,13 @@ fn generate_field_accessors(
let mut serialized = core::array::ArrayTrait::new();
$serialized_param_keys$
let values = dojo::model::Model::<$model_name$>::get_member(
let mut values = dojo::model::Model::<$model_name$>::get_member(
world,
serialized.span(),
$field_selector$
);
let mut serialized = core::array::ArrayTrait::new();
core::array::serialize_array_helper(values, ref serialized);
let mut serialized = core::array::ArrayTrait::span(@serialized);
let field_value = core::serde::Serde::<$field_type$>::deserialize(ref serialized);
let field_value = core::serde::Serde::<$field_type$>::deserialize(ref values);
if core::option::OptionTrait::<$field_type$>::is_none(@field_value) {
panic!(
Expand Down Expand Up @@ -859,17 +859,12 @@ fn generate_entity_field_accessors(model_name: String, member: &Member) -> Rewri
"
fn get_$field_name$(world: dojo::world::IWorldDispatcher, entity_id: felt252) -> $field_type$ \
{
let values = dojo::model::ModelEntity::<$model_name$Entity>::get_member(
let mut values = dojo::model::ModelEntity::<$model_name$Entity>::get_member(
world,
entity_id,
$field_selector$
);
let mut serialized = core::array::ArrayTrait::new();
core::array::serialize_array_helper(values, ref serialized);
let mut serialized = core::array::ArrayTrait::span(@serialized);
let field_value = core::serde::Serde::<$field_type$>::deserialize(ref serialized);
let field_value = core::serde::Serde::<$field_type$>::deserialize(ref values);
if core::option::OptionTrait::<$field_type$>::is_none(@field_value) {
panic!(
Expand Down
8 changes: 4 additions & 4 deletions examples/game-lib/armory/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[derive(Drop, Serde)]
#[dojo::model(namespace: "armory")]
struct Flatbow {
pub struct Flatbow {
#[key]
id: u32,
atk_speek: u32,
range: u32,
pub id: u32,
pub atk_speek: u32,
pub range: u32,
}
10 changes: 5 additions & 5 deletions examples/game-lib/bestiary/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[derive(Drop, Serde)]
#[dojo::model(namespace: "bestiary")]
struct RiverSkale {
pub struct RiverSkale {
#[key]
id: u32,
health: u32,
armor: u32,
attack: u32,
pub id: u32,
pub health: u32,
pub armor: u32,
pub attack: u32,
}
4 changes: 2 additions & 2 deletions examples/spawn-and-move/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "dojo_examples"
version = "0.7.3"
# Use the prelude with the less imports as possible
# from corelib.
edition = "2023_10"
edition = "2023_11"

[cairo]
sierra-replace-ids = true
Expand Down Expand Up @@ -43,7 +43,7 @@ rpc_url = "http://localhost:5050/"
# Default account for katana with seed = 0
account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address = "0x5377ecb8b7b6ce3f17daf9064a5f5ee7f6642c3e72579b45478a828007db355"
world_address = "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6"

[profile.release.tool.dojo]
# for more info on how `merge-strategy` works see:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "DojoContract"
class_hash = "0x19d49d8d67dd2e6f73c93eb886105d445ac6983a49950831a325872f8c662be"
original_class_hash = "0x19d49d8d67dd2e6f73c93eb886105d445ac6983a49950831a325872f8c662be"
class_hash = "0x59dac0f4034cbf09380196825991536fb07a7b6b692c3d0c0e5d5fe4a754966"
original_class_hash = "0x59dac0f4034cbf09380196825991536fb07a7b6b692c3d0c0e5d5fe4a754966"
base_class_hash = "0x0"
abi = "manifests/dev/base/abis/contracts/dojo_examples-actions-40b6994c.json"
reads = []
Expand Down
4 changes: 2 additions & 2 deletions examples/spawn-and-move/manifests/dev/base/dojo-world.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kind = "Class"
class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557"
original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557"
class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb"
original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb"
abi = "manifests/dev/base/abis/dojo-world.json"
tag = "dojo-world"
manifest_name = "dojo-world"
Loading

0 comments on commit 1a55dd6

Please sign in to comment.