Skip to content

Commit

Permalink
Update sozo model (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Sep 27, 2023
1 parent 554c7e1 commit b64e5aa
Show file tree
Hide file tree
Showing 23 changed files with 298 additions and 146 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions crates/dojo-lang/src/manifest_test_data/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ test_manifest_file
"key": false
}
],
"class_hash": "0x3682ef3ef44d8db8c8bfe72533e2e3b17e7b80efd6550b365deed7b4b3f8597",
"class_hash": "0x3dedf3d747db10aee693c0d034fcde1064ec66c84d285e60630f860dc9df566",
"abi": [
{
"type": "function",
Expand All @@ -819,7 +819,18 @@ test_manifest_file
},
{
"type": "function",
"name": "size",
"name": "unpacked_size",
"inputs": [],
"outputs": [
{
"type": "core::integer::u32"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "packed_size",
"inputs": [],
"outputs": [
{
Expand Down Expand Up @@ -956,7 +967,7 @@ test_manifest_file
"key": false
}
],
"class_hash": "0x69889772f44397619cd8965660e1c8e80ba5f0c917ba40df29b2ffa5b440745",
"class_hash": "0x1b5f19668b9299cea232978c59856b16da649e6aa4dfc3f2a42aa8435e06bc7",
"abi": [
{
"type": "function",
Expand All @@ -971,7 +982,18 @@ test_manifest_file
},
{
"type": "function",
"name": "size",
"name": "unpacked_size",
"inputs": [],
"outputs": [
{
"type": "core::integer::u32"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "packed_size",
"inputs": [],
"outputs": [
{
Expand Down
10 changes: 9 additions & 1 deletion crates/dojo-lang/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@ pub fn handle_model_struct(
}
#[external(v0)]
fn size(self: @ContractState) -> usize {
fn unpacked_size(self: @ContractState) -> usize {
dojo::database::schema::SchemaIntrospection::<$type_name$>::size()
}
#[external(v0)]
fn packed_size(self: @ContractState) -> usize {
let mut layout = ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<$type_name$>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}
#[external(v0)]
fn layout(self: @ContractState) -> Span<u8> {
let mut layout = ArrayTrait::new();
Expand Down
10 changes: 9 additions & 1 deletion crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,18 @@ mod position {
}

#[external(v0)]
fn size(self: @ContractState) -> usize {
fn unpacked_size(self: @ContractState) -> usize {
dojo::database::schema::SchemaIntrospection::<Position>::size()
}

#[external(v0)]
fn packed_size(self: @ContractState) -> usize {
let mut layout = ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<Position>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}

#[external(v0)]
fn layout(self: @ContractState) -> Span<u8> {
let mut layout = ArrayTrait::new();
Expand Down
30 changes: 27 additions & 3 deletions crates/dojo-lang/src/plugin_test_data/model
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,18 @@ mod position {
}

#[external(v0)]
fn size(self: @ContractState) -> usize {
fn unpacked_size(self: @ContractState) -> usize {
dojo::database::schema::SchemaIntrospection::<Position>::size()
}

#[external(v0)]
fn packed_size(self: @ContractState) -> usize {
let mut layout = ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<Position>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}

#[external(v0)]
fn layout(self: @ContractState) -> Span<u8> {
let mut layout = ArrayTrait::new();
Expand Down Expand Up @@ -294,10 +302,18 @@ mod roles {
}

#[external(v0)]
fn size(self: @ContractState) -> usize {
fn unpacked_size(self: @ContractState) -> usize {
dojo::database::schema::SchemaIntrospection::<Roles>::size()
}

#[external(v0)]
fn packed_size(self: @ContractState) -> usize {
let mut layout = ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<Roles>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}

#[external(v0)]
fn layout(self: @ContractState) -> Span<u8> {
let mut layout = ArrayTrait::new();
Expand Down Expand Up @@ -426,10 +442,18 @@ mod player {
}

#[external(v0)]
fn size(self: @ContractState) -> usize {
fn unpacked_size(self: @ContractState) -> usize {
dojo::database::schema::SchemaIntrospection::<Player>::size()
}

#[external(v0)]
fn packed_size(self: @ContractState) -> usize {
let mut layout = ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<Player>::layout(ref layout);
let mut layout_span = layout.span();
dojo::packing::calculate_packed_size(ref layout_span)
}

#[external(v0)]
fn layout(self: @ContractState) -> Span<u8> {
let mut layout = ArrayTrait::new();
Expand Down
2 changes: 2 additions & 0 deletions crates/dojo-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ version = "0.2.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hex = "0.4.3"
serde.workspace = true
starknet.workspace = true
thiserror.workspace = true
12 changes: 6 additions & 6 deletions crates/dojo-types/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ pub struct Member {

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Ty {
Name(String),
Terminal(String),
Struct(Struct),
Enum(Enum),
}

impl Ty {
pub fn name(&self) -> String {
match self {
Ty::Name(s) => s.clone(),
Ty::Terminal(s) => s.clone(),
Ty::Struct(s) => s.name.clone(),
Ty::Enum(e) => e.name.clone(),
}
Expand All @@ -33,7 +33,7 @@ impl Ty {
fn flatten_ty(ty: Ty) -> Vec<Ty> {
let mut items = vec![];
match ty {
Ty::Name(_) => {
Ty::Terminal(_) => {
items.push(ty.clone());
}
Ty::Struct(mut s) => {
Expand All @@ -48,7 +48,7 @@ impl Ty {
_ => {}
}

s.children[i].ty = Ty::Name(member.ty.name());
s.children[i].ty = Ty::Terminal(member.ty.name());
}

items.push(Ty::Struct(s))
Expand All @@ -65,7 +65,7 @@ impl Ty {
_ => {}
}

e.values[i] = Ty::Name(ty.name());
e.values[i] = Ty::Terminal(ty.name());
}

items.push(Ty::Enum(e))
Expand All @@ -83,7 +83,7 @@ impl std::fmt::Display for Ty {
let str = items
.iter()
.map(|ty| match ty {
Ty::Name(s) => s.to_string(),
Ty::Terminal(s) => s.to_string(),
Ty::Struct(s) => {
let mut struct_str = format!("struct {} {{\n", s.name);
for member in &s.children {
Expand Down
97 changes: 97 additions & 0 deletions crates/dojo-types/src/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::str::FromStr;

use starknet::core::types::FieldElement;

pub enum CairoType {
U8,
U16,
U32,
U64,
U128,
U256,
USize,
Bool,
ContractAddress,
ClassHash,
Felt252,
}

#[derive(Debug, thiserror::Error)]
pub enum CairoTypeError {
#[error("Value must have at least one FieldElement")]
MissingFieldElement,
#[error("Not enough FieldElements for U256")]
NotEnoughFieldElements,
#[error("Unsupported CairoType for SQL formatting")]
UnsupportedType,
}

impl FromStr for CairoType {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"u8" => Ok(CairoType::U8),
"u16" => Ok(CairoType::U16),
"u32" => Ok(CairoType::U32),
"u64" => Ok(CairoType::U64),
"u128" => Ok(CairoType::U128),
"u256" => Ok(CairoType::U256),
"usize" => Ok(CairoType::USize),
"bool" => Ok(CairoType::Bool),
"ContractAddress" => Ok(CairoType::ContractAddress),
"ClassHash" => Ok(CairoType::ClassHash),
"felt252" => Ok(CairoType::Felt252),
_ => Err(()),
}
}
}

impl CairoType {
pub fn to_sql_type(&self) -> String {
match self {
CairoType::U8
| CairoType::U16
| CairoType::U32
| CairoType::U64
| CairoType::USize
| CairoType::Bool => "INTEGER".to_string(),
CairoType::U128
| CairoType::U256
| CairoType::ContractAddress
| CairoType::ClassHash
| CairoType::Felt252 => "TEXT".to_string(),
}
}

pub fn format_for_sql(&self, value: Vec<&FieldElement>) -> Result<String, CairoTypeError> {
if value.is_empty() {
return Err(CairoTypeError::MissingFieldElement);
}

match self {
CairoType::U8
| CairoType::U16
| CairoType::U32
| CairoType::U64
| CairoType::USize
| CairoType::Bool => Ok(format!(", '{}'", value[0])),
CairoType::U128
| CairoType::ContractAddress
| CairoType::ClassHash
| CairoType::Felt252 => Ok(format!(", '{:0>64x}'", value[0])),
CairoType::U256 => {
if value.len() < 2 {
Err(CairoTypeError::NotEnoughFieldElements)
} else {
let mut buffer = [0u8; 32];
let value0_bytes = value[0].to_bytes_be();
let value1_bytes = value[1].to_bytes_be();
buffer[..16].copy_from_slice(&value0_bytes);
buffer[16..].copy_from_slice(&value1_bytes);
Ok(format!(", '{}'", hex::encode(buffer)))
}
}
}
}
}
1 change: 1 addition & 0 deletions crates/dojo-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod component;
pub mod core;
pub mod event;
pub mod storage;
pub mod system;
4 changes: 2 additions & 2 deletions crates/sozo/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use tracing_log::AsTrace;
use crate::commands::auth::AuthArgs;
use crate::commands::build::BuildArgs;
use crate::commands::completions::CompletionsArgs;
use crate::commands::component::ComponentArgs;
use crate::commands::dev::DevArgs;
use crate::commands::events::EventsArgs;
use crate::commands::execute::ExecuteArgs;
use crate::commands::init::InitArgs;
use crate::commands::migrate::MigrateArgs;
use crate::commands::model::ModelArgs;
use crate::commands::register::RegisterArgs;
use crate::commands::test::TestArgs;

Expand Down Expand Up @@ -64,7 +64,7 @@ pub enum Commands {
#[command(about = "Execute a world's system")]
Execute(ExecuteArgs),
#[command(about = "Interact with a worlds components")]
Component(ComponentArgs),
Model(ModelArgs),
#[command(about = "Register new systems and components")]
Register(RegisterArgs),
#[command(about = "Queries world events")]
Expand Down
4 changes: 2 additions & 2 deletions crates/sozo/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::args::Commands;
pub(crate) mod auth;
pub(crate) mod build;
pub(crate) mod completions;
pub(crate) mod component;
pub(crate) mod dev;
pub(crate) mod events;
pub(crate) mod execute;
pub(crate) mod init;
pub(crate) mod migrate;
pub(crate) mod model;
pub(crate) mod options;
pub(crate) mod register;
pub(crate) mod test;
Expand All @@ -28,7 +28,7 @@ pub fn run(command: Commands, config: &Config) -> Result<()> {
Commands::Dev(args) => args.run(config),
Commands::Auth(args) => args.run(config),
Commands::Execute(args) => args.run(config),
Commands::Component(args) => args.run(config),
Commands::Model(args) => args.run(config),
Commands::Register(args) => args.run(config),
Commands::Events(args) => args.run(config),
Commands::Completions(args) => args.run(),
Expand Down
Loading

0 comments on commit b64e5aa

Please sign in to comment.