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

refactor: update & fix unity bindgen #2631

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions crates/dojo/bindgen/src/plugins/unity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,36 @@
model_struct = Some(token.to_composite().unwrap());
continue;
}

out += UnityPlugin::format_struct(token.to_composite().unwrap()).as_str();
}

let model_struct = model_struct.expect("model struct not found");
handled_tokens
.iter()
.filter(|(_, s)| {
model_struct.inners.iter().any(|inner| {
inner.token.type_name() == s.type_name()
&& inner.token.type_name() != "ByteArray"
})
})
.for_each(|(_, s)| {
out += UnityPlugin::format_struct(s).as_str();
});

Check warning on line 272 in crates/dojo/bindgen/src/plugins/unity/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/unity/mod.rs#L261-L272

Added lines #L261 - L272 were not covered by tests

Comment on lines +261 to +273
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ohayo sensei! Consider improving error handling for model struct lookup.

While the filtering logic for handled tokens is well-implemented, the expect() call on line 261 could cause a panic if the model struct is not found. Consider using a more graceful error handling approach.

-        let model_struct = model_struct.expect("model struct not found");
+        let model_struct = model_struct.ok_or_else(|| {
+            anyhow::anyhow!("Model struct not found for {}", model.tag)
+        })?;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let model_struct = model_struct.expect("model struct not found");
handled_tokens
.iter()
.filter(|(_, s)| {
model_struct.inners.iter().any(|inner| {
inner.token.type_name() == s.type_name()
&& inner.token.type_name() != "ByteArray"
})
})
.for_each(|(_, s)| {
out += UnityPlugin::format_struct(s).as_str();
});
let model_struct = model_struct.ok_or_else(|| {
anyhow::anyhow!("Model struct not found for {}", model.tag)
})?;
handled_tokens
.iter()
.filter(|(_, s)| {
model_struct.inners.iter().any(|inner| {
inner.token.type_name() == s.type_name()
&& inner.token.type_name() != "ByteArray"
})
})
.for_each(|(_, s)| {
out += UnityPlugin::format_struct(s).as_str();
});

for token in &sorted_enums {
if handled_tokens.contains_key(&token.type_path()) {
continue;
}

handled_tokens.insert(token.type_path(), token.to_composite().unwrap().to_owned());
out += UnityPlugin::format_enum(token.to_composite().unwrap()).as_str();
if handled_tokens.iter().any(|(_, s)| s.type_name() == token.type_name()) {
out += UnityPlugin::format_enum(token.to_composite().unwrap()).as_str();
}

Check warning on line 282 in crates/dojo/bindgen/src/plugins/unity/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/unity/mod.rs#L280-L282

Added lines #L280 - L282 were not covered by tests
}

out += "\n";

out += UnityPlugin::format_model(
&get_namespace_from_tag(&model.tag),
model_struct.expect("model struct not found"),
)
.as_str();
out +=
UnityPlugin::format_model(&get_namespace_from_tag(&model.tag), model_struct).as_str();

Check warning on line 288 in crates/dojo/bindgen/src/plugins/unity/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/unity/mod.rs#L287-L288

Added lines #L287 - L288 were not covered by tests

out
}
Expand Down Expand Up @@ -477,7 +487,7 @@

return await account.ExecuteRaw(new dojo.Call[] {{
new dojo.Call{{
to = contractAddress,
to = new FieldElement(contractAddress).Inner,

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

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/unity/mod.rs#L490

Added line #L490 was not covered by tests
selector = \"{system_name}\",
calldata = calldata.ToArray()
}}
Expand Down
Loading