-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(sozo): adjust typescript bindgen to v1 #2202
Merged
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2469693
work on ts v1 bindgen
ponderingdemocritus c5fd99f
work on model def
ponderingdemocritus 69fd5cc
formatting
ponderingdemocritus fe5731e
improve codegen
ponderingdemocritus f89fee0
add tests
ponderingdemocritus b00100a
update enum to string
ponderingdemocritus ec106d9
fix mapping in systems gen
ponderingdemocritus 37b2ded
remove unneeded filters
ponderingdemocritus 9666103
fix imports and errors
ponderingdemocritus 010aa16
Merge remote-tracking branch 'dojo/main' into loaf-bindgen
glihm 6023603
fix: fix tests and fmt
glihm 2d04f58
chore: bump cainome
glihm 05c75cf
chore: move deps to dev deps
glihm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,11 @@ | |
|
||
fn generated_header() -> String { | ||
format!( | ||
"// Generated by dojo-bindgen on {}. Do not modify this file manually.\n", | ||
" | ||
// Generated by dojo-bindgen on {}. Do not modify this file manually. | ||
// Import the necessary types from the recs SDK | ||
// generate again with `sozo build --typescript` | ||
", | ||
chrono::Utc::now().to_rfc2822() | ||
) | ||
} | ||
|
@@ -118,7 +122,6 @@ | |
export interface {name} {{ | ||
{native_fields} | ||
}} | ||
|
||
export const {name}Definition = {{ | ||
{fields} | ||
}}; | ||
|
@@ -134,12 +137,24 @@ | |
// This will be formatted into a C# enum | ||
// Enum is mapped using index of cairo enum | ||
fn format_enum(token: &Composite) -> String { | ||
// filter out common types | ||
// TODO: Make cleaner | ||
if token.type_path == "core::option::Option::<core::integer::u32>" | ||
|| token.type_path == "core::option::Option::<core::integer::u8>" | ||
|| token.type_path == "core::option::Option::<core::integer::u16>" | ||
|| token.type_path == "core::option::Option::<core::integer::u64>" | ||
|| token.type_path == "core::option::Option::<core::integer::u128>" | ||
|| token.type_path == "core::option::Option::<core::integer::u256>" | ||
{ | ||
return String::new(); // Return an empty string for these enums | ||
} | ||
|
||
let name = TypescriptPlugin::map_type(&Token::Composite(token.clone())); | ||
|
||
let mut result = format!( | ||
" | ||
// Type definition for `{}` enum | ||
type {} = ", | ||
export type {} = ", | ||
token.type_path, name | ||
); | ||
|
||
|
@@ -168,7 +183,7 @@ | |
export const {name}Definition = {{ | ||
type: RecsType.String,{} | ||
}}; | ||
", | ||
", | ||
if !token.inners.is_empty() { | ||
"\n value: RecsType.String".to_string() | ||
} else { | ||
|
@@ -237,9 +252,8 @@ | |
out += TypescriptPlugin::generated_header().as_str(); | ||
out += "import { defineComponent, Type as RecsType, World } from \"@dojoengine/recs\";\n"; | ||
out += "\n"; | ||
out += "export type ContractComponents = Awaited< | ||
ReturnType<typeof defineContractComponents> | ||
>;\n"; | ||
out += "export type ContractComponents = Awaited<ReturnType<typeof \ | ||
defineContractComponents>>;\n"; | ||
|
||
out += "\n\n"; | ||
|
||
|
@@ -268,6 +282,14 @@ | |
} | ||
}); | ||
|
||
for token in &tokens.enums { | ||
if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 | ||
{ | ||
continue; | ||
} | ||
out += TypescriptPlugin::format_enum(token.to_composite().unwrap()).as_str(); | ||
} | ||
|
||
for token in &structs { | ||
if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 | ||
{ | ||
|
@@ -285,14 +307,6 @@ | |
out += TypescriptPlugin::format_struct(token.to_composite().unwrap()).as_str(); | ||
} | ||
|
||
for token in &tokens.enums { | ||
if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1 | ||
{ | ||
continue; | ||
} | ||
out += TypescriptPlugin::format_enum(token.to_composite().unwrap()).as_str(); | ||
} | ||
|
||
out += "\n"; | ||
} | ||
|
||
|
@@ -314,7 +328,20 @@ | |
// Formats a system into a C# method used by the contract class | ||
// Handled tokens should be a list of all structs and enums used by the contract | ||
// Such as a set of referenced tokens from a model | ||
fn format_system(system: &Function, handled_tokens: &[Composite]) -> String { | ||
fn format_system(system: &Function, handled_tokens: &[Composite], namespace: String) -> String { | ||
if [ | ||
"contract_name", | ||
"namespace", | ||
"tag", | ||
"name_hash", | ||
"selector", | ||
"dojo_init", | ||
"namespace_hash", | ||
] | ||
.contains(&system.name.as_str()) | ||
{ | ||
return String::new(); | ||
} | ||
Comment on lines
+347
to
+360
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohayo sensei! Optimize the filtering mechanism for systems. Consider using a set for cleaner and more efficient filtering of system names. let excluded_systems: HashSet<&str> = [
"contract_name",
"namespace",
"tag",
"name_hash",
"selector",
"dojo_init",
"namespace_hash",
].iter().cloned().collect();
if excluded_systems.contains(system.name.as_str()) {
return String::new();
} |
||
fn map_type(token: &Token) -> String { | ||
match token { | ||
Token::CoreBasic(_) => TypescriptPlugin::map_type(token) | ||
|
@@ -329,6 +356,18 @@ | |
let args = system | ||
.inputs | ||
.iter() | ||
.filter(|arg| { | ||
![ | ||
"contract_name", | ||
"namespace", | ||
"tag", | ||
"name_hash", | ||
"selector", | ||
"dojo_init", | ||
"namespace_hash", | ||
] | ||
.contains(&arg.0.as_str()) | ||
}) | ||
.map(|arg| format!("{}: {}", arg.0, map_type(&arg.1))) | ||
.collect::<Vec<String>>() | ||
.join(", "); | ||
|
@@ -395,6 +434,18 @@ | |
let calldata = system | ||
.inputs | ||
.iter() | ||
.filter(|arg| { | ||
![ | ||
"contract_name", | ||
"namespace", | ||
"tag", | ||
"name_hash", | ||
"selector", | ||
"dojo_init", | ||
"namespace_hash", | ||
] | ||
.contains(&arg.0.as_str()) | ||
}) | ||
.map(|arg| handle_arg_recursive(&arg.0, &arg.1, handled_tokens)) | ||
.collect::<Vec<String>>() | ||
.join(",\n "); | ||
|
@@ -408,7 +459,8 @@ | |
props.account, | ||
contract_name, | ||
\"{system_name}\", | ||
[{calldata}] | ||
[{calldata}], | ||
\"{namespace}\" | ||
); | ||
}} catch (error) {{ | ||
console.error(\"Error executing spawn:\", error); | ||
|
@@ -423,14 +475,19 @@ | |
// formatted args to use our mapped types | ||
args = args, | ||
// calldata for execute | ||
calldata = calldata | ||
calldata = calldata, | ||
namespace = TypescriptPlugin::get_namespace_from_tag(&namespace) | ||
) | ||
} | ||
|
||
// Formats a contract tag into a pretty contract name | ||
// eg. dojo_examples-actions -> Actions | ||
// eg. dojo_examples-actions -> actions | ||
fn formatted_contract_name(tag: &str) -> String { | ||
naming::capitalize(&naming::get_name_from_tag(tag)) | ||
naming::get_name_from_tag(tag) | ||
} | ||
|
||
fn get_namespace_from_tag(tag: &str) -> String { | ||
tag.split('-').next().unwrap_or(tag).to_string() | ||
} | ||
|
||
// Handles a contract definition and its underlying systems | ||
|
@@ -458,8 +515,25 @@ | |
let systems = contract | ||
.systems | ||
.iter() | ||
.filter(|system| { | ||
let name = system.to_function().unwrap().name.as_str(); | ||
![ | ||
"contract_name", | ||
"namespace", | ||
"tag", | ||
"name_hash", | ||
"selector", | ||
"dojo_init", | ||
"namespace_hash", | ||
] | ||
.contains(&name) | ||
}) | ||
.map(|system| { | ||
TypescriptPlugin::format_system(system.to_function().unwrap(), handled_tokens) | ||
TypescriptPlugin::format_system( | ||
system.to_function().unwrap(), | ||
handled_tokens, | ||
contract.tag.clone(), | ||
) | ||
}) | ||
.collect::<Vec<String>>() | ||
.join("\n\n "); | ||
|
@@ -485,6 +559,19 @@ | |
contract | ||
.systems | ||
.iter() | ||
.filter(|system| { | ||
let name = system.to_function().unwrap().name.as_str(); | ||
![ | ||
"contract_name", | ||
"namespace", | ||
"tag", | ||
"name_hash", | ||
"selector", | ||
"dojo_init", | ||
"namespace_hash", | ||
] | ||
.contains(&name) | ||
}) | ||
.map(|system| { system.to_function().unwrap().name.to_string() }) | ||
.collect::<Vec<String>>() | ||
.join(", ") | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohayo sensei! Optimize the filtering mechanism for enums.
Consider using a set for cleaner and more efficient filtering of
Option
types.