Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed Jul 9, 2024
1 parent 82a2a16 commit fdd32ce
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 1,005 deletions.
Binary file added ._target
Binary file not shown.
25 changes: 7 additions & 18 deletions crates/dojo-core/src/model.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dojo::world::IWorldDispatcher;
use dojo::world::{IWorldDispatcher, ModelIndex, ModelValue};
use starknet::SyscallResult;

/// Trait that is implemented at Cairo level for each struct that is a model.
Expand All @@ -11,34 +11,23 @@ trait ModelValues<T> {
}

trait Model<T> {
fn entity(
world: IWorldDispatcher, keys: Span<felt252>, layout: dojo::database::introspect::Layout
) -> T;

fn set_entity(
world: IWorldDispatcher,
keys: Span<felt252>,
values: Span<felt252>,
layout: dojo::database::introspect::Layout
);

fn delete_entity(
world: IWorldDispatcher, keys: Span<felt252>, layout: dojo::database::introspect::Layout
);
fn entity(world: IWorldDispatcher, keys: Span<felt252>) -> T;
// Note: `get` is implemented with a generated trait because it takes
// the list of model keys as separated parameters.
fn set(self: @T, world: IWorldDispatcher);
fn delete(self: @T, world: IWorldDispatcher);

fn entity_member(
world: IWorldDispatcher,
keys: Span<felt252>,
member_id: felt252,
layout: dojo::database::introspect::Layout
) -> Span<felt252>;

fn set_entity_member(
self: @T,
world: IWorldDispatcher,
keys: Span<felt252>,
member_id: felt252,
values: Span<felt252>,
layout: dojo::database::introspect::Layout
);

/// Returns the name of the model as it was written in Cairo code.
Expand Down
67 changes: 41 additions & 26 deletions crates/dojo-core/src/resource_metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! Manually expand to ensure that dojo-core
//! does not depend on dojo plugin to be built.
//!
use dojo::world::{IWorldDispatcherTrait};
use dojo::world::{IWorldDispatcherTrait, ModelIndex};
use dojo::model::Model;
use dojo::utils;

fn initial_address() -> starknet::ContractAddress {
starknet::contract_address_const::<0>()
Expand All @@ -26,10 +27,9 @@ struct ResourceMetadata {
impl ResourceMetadataModel of dojo::model::Model<ResourceMetadata> {
fn entity(
world: dojo::world::IWorldDispatcher,
keys: Span<felt252>,
layout: dojo::database::introspect::Layout
keys: Span<felt252>
) -> ResourceMetadata {
let values = world.entity(Self::selector(), keys, layout);
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);
Expand All @@ -45,46 +45,61 @@ impl ResourceMetadataModel of dojo::model::Model<ResourceMetadata> {
core::option::OptionTrait::<ResourceMetadata>::unwrap(entity)
}

fn set_entity(
fn set(
self: @ResourceMetadata,
world: dojo::world::IWorldDispatcher,
keys: Span<felt252>,
values: Span<felt252>,
layout: dojo::database::introspect::Layout
) {
dojo::world::IWorldDispatcherTrait::set_entity(
world, Self::selector(), keys, values, layout
world, Self::selector(), ModelIndex::Keys(self.keys()), self.values(), Self::layout()
);
}

fn delete_entity(
fn delete(
self: @ResourceMetadata,
world: dojo::world::IWorldDispatcher,
keys: Span<felt252>,
layout: dojo::database::introspect::Layout
) {
dojo::world::IWorldDispatcherTrait::delete_entity(world, Self::selector(), keys, layout);
world.delete_entity(
Self::selector(),
ModelIndex::Keys(self.keys()),
Self::layout()
);
}

fn entity_member(
world: dojo::world::IWorldDispatcher,
keys: Span<felt252>,
member_id: felt252,
layout: dojo::database::introspect::Layout
member_id: felt252
) -> Span<felt252> {
dojo::world::IWorldDispatcherTrait::entity_member(
world, Self::selector(), keys, member_id, layout
)
match utils::find_model_field_layout(Self::layout(), member_id) {
Option::Some(field_layout) => {
let entity_id = utils::entity_id_from_keys(keys);
world.entity(
Self::selector(),
ModelIndex::MemberId((entity_id, member_id)),
field_layout
)
},
Option::None => panic_with_felt252('bad member id')
}
}

fn set_entity_member(
self: @ResourceMetadata,
world: dojo::world::IWorldDispatcher,
keys: Span<felt252>,
member_id: felt252,
values: Span<felt252>,
layout: dojo::database::introspect::Layout
values: Span<felt252>
) {
dojo::world::IWorldDispatcherTrait::set_entity_member(
world, Self::selector(), keys, member_id, values, layout
);
match utils::find_model_field_layout(Self::layout(), member_id) {
Option::Some(field_layout) => {
world.set_entity(
Self::selector(),
ModelIndex::MemberId((self.entity_id(), member_id)),
values,
field_layout
)
},
Option::None => panic_with_felt252('bad member id')
}
}

#[inline(always)]
Expand Down Expand Up @@ -116,11 +131,11 @@ impl ResourceMetadataModel of dojo::model::Model<ResourceMetadata> {
}

fn name_hash() -> felt252 {
dojo::utils::hash(@Self::name())
utils::hash(@Self::name())
}

fn namespace_hash() -> felt252 {
dojo::utils::hash(@Self::namespace())
utils::hash(@Self::namespace())
}

#[inline(always)]
Expand Down
30 changes: 30 additions & 0 deletions crates/dojo-core/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,33 @@ fn hash(data: @ByteArray) -> felt252 {
fn entity_id_from_keys(keys: Span<felt252>) -> felt252 {
poseidon::poseidon_hash_span(keys)
}

/// TODO
fn find_model_field_layout(
model_layout: dojo::database::introspect::Layout,
member_id: felt252
) -> Option<dojo::database::introspect::Layout> {
match model_layout {
dojo::database::introspect::Layout::Struct(struct_layout) => {
let mut i = 0;
let layout = loop {
if i >= struct_layout.len() {
break Option::None;
}

let field_layout = *struct_layout.at(i);
if field_layout.selector == member_id {
break Option::Some(field_layout.layout);
}
i += 1;
};

layout
},
_ => {
// should never happen as model layouts are always struct layouts.
panic_with_felt252('Unexpected model layout');
Option::None
}
}
}
Loading

0 comments on commit fdd32ce

Please sign in to comment.