Skip to content
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 13 commits into from
Jul 25, 2024
129 changes: 108 additions & 21 deletions crates/dojo-bindgen/src/plugins/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
",

Check warning on line 89 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L85-L89

Added lines #L85 - L89 were not covered by tests
chrono::Utc::now().to_rfc2822()
)
}
Expand Down Expand Up @@ -118,7 +122,6 @@
export interface {name} {{
{native_fields}
}}

export const {name}Definition = {{
{fields}
}};
Expand All @@ -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>"

Check warning on line 147 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L140-L147

Added lines #L140 - L147 were not covered by tests
{
return String::new(); // Return an empty string for these enums
}
Comment on lines +143 to +153
Copy link

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.

let excluded_types: HashSet<&str> = [
    "core::option::Option::<core::integer::u32>",
    "core::option::Option::<core::integer::u8>",
    "core::option::Option::<core::integer::u16>",
    "core::option::Option::<core::integer::u64>",
    "core::option::Option::<core::integer::u128>",
    "core::option::Option::<core::integer::u256>",
].iter().cloned().collect();

if excluded_types.contains(token.type_path.as_str()) {
    return String::new(); // Return an empty string for these enums
}


Check warning on line 151 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L149-L151

Added lines #L149 - L151 were not covered by tests
let name = TypescriptPlugin::map_type(&Token::Composite(token.clone()));

let mut result = format!(
"
// Type definition for `{}` enum
type {} = ",
export type {} = ",

Check warning on line 157 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L157

Added line #L157 was not covered by tests
token.type_path, name
);

Expand Down Expand Up @@ -168,7 +183,7 @@
export const {name}Definition = {{
type: RecsType.String,{}
}};
",
",

Check warning on line 186 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L186

Added line #L186 was not covered by tests
if !token.inners.is_empty() {
"\n value: RecsType.String".to_string()
} else {
Expand Down Expand Up @@ -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";

Check warning on line 256 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L255-L256

Added lines #L255 - L256 were not covered by tests

out += "\n\n";

Expand Down Expand Up @@ -268,6 +282,14 @@
}
});

for token in &tokens.enums {
if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1

Check warning on line 286 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L285-L286

Added lines #L285 - L286 were not covered by tests
{
continue;
}
out += TypescriptPlugin::format_enum(token.to_composite().unwrap()).as_str();

Check warning on line 290 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L288-L290

Added lines #L288 - L290 were not covered by tests
}

for token in &structs {
if handled_tokens.iter().filter(|t| t.type_name() == token.type_name()).count() > 1
{
Expand All @@ -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";
}

Expand All @@ -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())

Check warning on line 341 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L331-L341

Added lines #L331 - L341 were not covered by tests
{
return String::new();
}

Check warning on line 344 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L343-L344

Added lines #L343 - L344 were not covered by tests
Comment on lines +347 to +360
Copy link

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 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)
Expand All @@ -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())
})

Check warning on line 370 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L359-L370

Added lines #L359 - L370 were not covered by tests
.map(|arg| format!("{}: {}", arg.0, map_type(&arg.1)))
.collect::<Vec<String>>()
.join(", ");
Expand Down Expand Up @@ -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())
})

Check warning on line 448 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L437-L448

Added lines #L437 - L448 were not covered by tests
.map(|arg| handle_arg_recursive(&arg.0, &arg.1, handled_tokens))
.collect::<Vec<String>>()
.join(",\n ");
Expand All @@ -408,7 +459,8 @@
props.account,
contract_name,
\"{system_name}\",
[{calldata}]
[{calldata}],
\"{namespace}\"

Check warning on line 463 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L462-L463

Added lines #L462 - L463 were not covered by tests
);
}} catch (error) {{
console.error(\"Error executing spawn:\", error);
Expand All @@ -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)

Check warning on line 479 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L479

Added line #L479 was not covered by tests
)
}

// 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)
}

Check warning on line 487 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L486-L487

Added lines #L486 - L487 were not covered by tests

fn get_namespace_from_tag(tag: &str) -> String {
tag.split('-').next().unwrap_or(tag).to_string()

Check warning on line 490 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L489-L490

Added lines #L489 - L490 were not covered by tests
}

// Handles a contract definition and its underlying systems
Expand Down Expand Up @@ -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)
})

Check warning on line 530 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L518-L530

Added lines #L518 - L530 were not covered by tests
.map(|system| {
TypescriptPlugin::format_system(system.to_function().unwrap(), handled_tokens)
TypescriptPlugin::format_system(
system.to_function().unwrap(),
handled_tokens,
contract.tag.clone(),
)

Check warning on line 536 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L532-L536

Added lines #L532 - L536 were not covered by tests
})
.collect::<Vec<String>>()
.join("\n\n ");
Expand All @@ -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)
})

Check warning on line 574 in crates/dojo-bindgen/src/plugins/typescript/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo-bindgen/src/plugins/typescript/mod.rs#L562-L574

Added lines #L562 - L574 were not covered by tests
.map(|system| { system.to_function().unwrap().name.to_string() })
.collect::<Vec<String>>()
.join(", ")
Expand Down
Loading