Skip to content

Commit

Permalink
Rename some usages of component to model (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Sep 27, 2023
1 parent 3eda043 commit 03c2f5b
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/dojo-core/src/executor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet::class_hash::Felt252TryIntoClassHash;
use dojo::executor::{executor, IExecutorDispatcher, IExecutorDispatcherTrait};
use dojo::world::{IWorldDispatcher};

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Foo {
#[key]
id: felt252,
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod database_test;
mod executor;
#[cfg(test)]
mod executor_test;
mod component;
mod model;
mod packing;
#[cfg(test)]
mod packing_test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
trait Component<T> {
trait Model<T> {
fn name(self: @T) -> felt252;
fn keys(self: @T) -> Span<felt252>;
fn values(self: @T) -> Span<felt252>;
fn layout(self: @T) -> Span<u8>;
}

#[starknet::interface]
trait IComponent<T> {
trait IModel<T> {
fn name(self: @T) -> felt252;
fn layout(self: @T) -> Span<felt252>;
fn schema(self: @T) -> Span<dojo::database::schema::Member>;
}

#[starknet::interface]
trait ISystem<T> {
fn name(self: @T) -> felt252;
}
4 changes: 2 additions & 2 deletions crates/dojo-core/src/world_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use dojo::test_utils::{spawn_test_world, deploy_with_world_address};

// Components and Systems

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Foo {
#[key]
caller: ContractAddress,
a: felt252,
b: u128,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Fizz {
#[key]
caller: ContractAddress,
Expand Down
10 changes: 5 additions & 5 deletions crates/dojo-defi/src/market/components.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::ContractAddress;
use dojo::component::StorageIntrospection;
use dojo::model::StorageIntrospection;

// Cubit fixed point math library
use cubit::f128::types::fixed::Fixed;
Expand All @@ -24,14 +24,14 @@ impl SchemaIntrospectionFixed of SchemaIntrospection<Fixed> {
}
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Cash {
#[key]
player: ContractAddress,
amount: u128,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Item {
#[key]
player: ContractAddress,
Expand All @@ -40,7 +40,7 @@ struct Item {
quantity: u128,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Liquidity {
#[key]
player: ContractAddress,
Expand All @@ -49,7 +49,7 @@ struct Liquidity {
shares: Fixed,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Market {
#[key]
item_id: u32,
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-erc/src/token/erc20_components.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC20Balance {
#[key]
token: ContractAddress,
Expand All @@ -9,7 +9,7 @@ struct ERC20Balance {
amount: u256,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC20Allowance {
#[key]
token: ContractAddress,
Expand All @@ -20,7 +20,7 @@ struct ERC20Allowance {
amount: u256,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC20Meta {
#[key]
token: ContractAddress,
Expand Down
10 changes: 5 additions & 5 deletions crates/dojo-erc/src/token/erc721/components.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC721Meta {
#[key]
token: ContractAddress,
Expand All @@ -9,7 +9,7 @@ struct ERC721Meta {
base_uri: felt252,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC721OperatorApproval {
#[key]
token: ContractAddress,
Expand All @@ -20,7 +20,7 @@ struct ERC721OperatorApproval {
approved: bool
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC721Owner {
#[key]
token: ContractAddress,
Expand All @@ -29,7 +29,7 @@ struct ERC721Owner {
address: ContractAddress
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC721Balance {
#[key]
token: ContractAddress,
Expand All @@ -38,7 +38,7 @@ struct ERC721Balance {
amount: u256,
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct ERC721TokenApproval {
#[key]
token: ContractAddress,
Expand Down
10 changes: 5 additions & 5 deletions crates/dojo-lang/src/inline_macros/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl InlineMacroExprPlugin for SetMacro {
return InlinePluginResult {
code: None,
diagnostics: vec![PluginDiagnostic {
message: "Invalid arguments: No components provided.".to_string(),
message: "Invalid arguments: No models provided.".to_string(),
stable_ptr: arg_list.args(db).stable_ptr().untyped(),
}],
};
Expand All @@ -77,10 +77,10 @@ impl InlineMacroExprPlugin for SetMacro {
for entity in bundle {
builder.add_str(&format!(
"\n let __set_macro_value__ = {};
{}.set_entity(dojo::component::Component::name(@__set_macro_value__), \
dojo::component::Component::keys(@__set_macro_value__), 0_u8, \
dojo::component::Component::values(@__set_macro_value__), \
dojo::component::Component::layout(@__set_macro_value__));",
{}.set_entity(dojo::model::Model::name(@__set_macro_value__), \
dojo::model::Model::keys(@__set_macro_value__), 0_u8, \
dojo::model::Model::values(@__set_macro_value__), \
dojo::model::Model::layout(@__set_macro_value__));",
entity,
world.as_syntax_node().get_text(db),
));
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//!
//! Learn more at [dojoengine.gg](http://dojoengine.gg).
pub mod compiler;
pub mod component;
pub mod inline_macros;
pub mod introspect;
mod manifest;
pub mod model;
pub mod plugin;
pub mod print;
pub mod system;
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Manifest {
module_id: ModuleId,
compiled_classes: &HashMap<SmolStr, (FieldElement, Option<abi::Contract>)>,
) {
for component in &aux_data.components {
for component in &aux_data.models {
let component = component.clone();
let name: SmolStr = component.name.clone().into();
if let Ok(Some(ModuleItemId::Struct(_))) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use convert_case::{Case, Casing};
use dojo_world::manifest::Member;

use crate::introspect::handle_introspect_struct;
use crate::plugin::{Component, DojoAuxData};
use crate::plugin::{DojoAuxData, Model};

/// A handler for Dojo code that modifies a component struct.
/// A handler for Dojo code that modifies a model struct.
/// Parameters:
/// * db: The semantic database.
/// * struct_ast: The AST of the component struct.
/// * struct_ast: The AST of the model struct.
/// Returns:
/// * A RewriteNode containing the generated code.
pub fn handle_component_struct(
pub fn handle_model_struct(
db: &dyn SyntaxGroup,
aux_data: &mut DojoAuxData,
struct_ast: ItemStruct,
Expand All @@ -38,7 +38,7 @@ pub fn handle_component_struct(

if keys.is_empty() {
diagnostics.push(PluginDiagnostic {
message: "Component must define atleast one #[key] attribute".into(),
message: "Model must define atleast one #[key] attribute".into(),
stable_ptr: struct_ast.name(db).stable_ptr().untyped(),
});
}
Expand Down Expand Up @@ -68,12 +68,12 @@ pub fn handle_component_struct(
members.iter().filter_map(|m| serialize_member(m, false)).collect::<_>();

let name = struct_ast.name(db).text(db);
aux_data.components.push(Component { name: name.to_string(), members: members.to_vec() });
aux_data.models.push(Model { name: name.to_string(), members: members.to_vec() });

(
RewriteNode::interpolate_patched(
"
impl $type_name$Component of dojo::component::Component<$type_name$> {
impl $type_name$Model of dojo::model::Model<$type_name$> {
#[inline(always)]
fn name(self: @$type_name$) -> felt252 {
'$type_name$'
Expand Down
14 changes: 7 additions & 7 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ use semver::Version;
use smol_str::SmolStr;
use url::Url;

use crate::component::handle_component_struct;
use crate::inline_macros::emit::EmitMacro;
use crate::inline_macros::get::GetMacro;
use crate::inline_macros::set::SetMacro;
use crate::introspect::handle_introspect_struct;
use crate::model::handle_model_struct;
use crate::print::derive_print;
use crate::system::System;

const SYSTEM_ATTR: &str = "system";

#[derive(Clone, Debug, PartialEq)]
pub struct Component {
pub struct Model {
pub name: String,
pub members: Vec<Member>,
}
Expand All @@ -46,8 +46,8 @@ pub struct SystemAuxData {
/// Dojo related auxiliary data of the Dojo plugin.
#[derive(Debug, Default, PartialEq)]
pub struct DojoAuxData {
/// A list of components that were processed by the plugin.
pub components: Vec<Component>,
/// A list of models that were processed by the plugin.
pub models: Vec<Model>,
/// A list of systems that were processed by the plugin and their component dependencies.
pub systems: Vec<SystemAuxData>,
}
Expand Down Expand Up @@ -150,13 +150,13 @@ impl MacroPlugin for DojoPlugin {
continue;
};

// Get the text of the segment and check if it is "Component"
// Get the text of the segment and check if it is "Model"
let derived = segment.ident(db).text(db);

match derived.as_str() {
"Component" => {
"Model" => {
let (component_rewrite_nodes, component_diagnostics) =
handle_component_struct(db, &mut aux_data, struct_ast.clone());
handle_model_struct(db, &mut aux_data, struct_ast.clone());
rewrite_nodes.push(component_rewrite_nodes);
diagnostics.extend(component_diagnostics);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/plugin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cairo_lang_test_utils::test_file_test!(
expand_plugin,
"src/plugin_test_data",
{
component: "component",
model: "model",
print: "print",
introspect: "introspect",
system: "system",
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct Vec2 {
y: u32
}

#[derive(Component, Copy, Drop, Print, Introspect)]
#[derive(Model, Copy, Drop, Print, Introspect)]
struct Position {
#[key]
player: ContractAddress,
Expand Down Expand Up @@ -81,13 +81,13 @@ impl Vec2SchemaIntrospection of dojo::database::schema::SchemaIntrospection<Vec2



#[derive(Component, Copy, Drop, Print, Introspect)]
#[derive(Model, Copy, Drop, Print, Introspect)]
struct Position {
#[key]
player: ContractAddress,
vec: Vec2,
}
impl PositionComponent of dojo::component::Component<Position> {
impl PositionModel of dojo::model::Model<Position> {
#[inline(always)]
fn name(self: @Position) -> felt252 {
'Position'
Expand Down
Loading

0 comments on commit 03c2f5b

Please sign in to comment.