-
Notifications
You must be signed in to change notification settings - Fork 172
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughOhayo, sensei! The changes in this pull request focus on the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant UnityPlugin
participant ModelStruct
participant CSharpCode
User->>UnityPlugin: Request code generation
UnityPlugin->>ModelStruct: Check for model struct
alt Model struct found
UnityPlugin->>UnityPlugin: Filter tokens
UnityPlugin->>CSharpCode: Format relevant tokens
UnityPlugin->>CSharpCode: Wrap contract address in FieldElement
else Model struct not found
UnityPlugin-->>User: Error: Model struct not found
end
UnityPlugin-->>User: Return generated C# code
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
Actionable comments posted: 1
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
crates/dojo/bindgen/src/plugins/unity/mod.rs
(2 hunks)
🔇 Additional comments (3)
crates/dojo/bindgen/src/plugins/unity/mod.rs (3)
280-282
: LGTM! Proper enum filtering implementation.
The check ensures that only enums referenced by the model structs are formatted, maintaining consistency with the struct handling approach.
287-288
: LGTM! Clean model formatting with proper namespace.
The implementation correctly uses the namespace from tag and formats the model using the filtered struct.
490-490
: LGTM! Proper contract address wrapping.
The contract address is correctly wrapped in FieldElement, ensuring proper type conversion for Starknet interaction.
Let's verify that all contract address usages are consistently wrapped:
✅ Verification successful
Let me modify the search to look for specific patterns in both C# and Rust code to verify the contract address handling consistency. The previous regex was too complex and unsupported. This will help us see the surrounding context of how contract addresses are used.
Contract address wrapping is correctly implemented, sensei!
The verification shows that contract addresses are properly handled across the codebase:
- In Unity (C#), the contract address is wrapped with FieldElement as seen in the reviewed line
- In TypeScript bindings, contract addresses are handled as strings and properly converted during execution
- In GraphQL/Rust code, addresses are consistently handled as strings for external interfaces
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for any direct contract address usage without FieldElement wrapping
rg -g '*.cs' 'contractAddress(?!.*FieldElement)'
Length of output: 302
Script:
#!/bin/bash
# Search for contract address usage patterns with proper context
rg --pcre2 "contractAddress" -g "*.cs" -B2 -A2
# Also search in Rust code to understand the context better
rg --pcre2 "contractAddress" -g "*.rs" -B2 -A2
Length of output: 9190
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(); | ||
}); | ||
|
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.
🛠️ 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.
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(); | |
}); |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2631 +/- ##
==========================================
- Coverage 56.91% 56.88% -0.03%
==========================================
Files 397 397
Lines 49467 49476 +9
==========================================
- Hits 28153 28144 -9
- Misses 21314 21332 +18 ☔ View full report in Codecov by Sentry. |
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.
Now the tokens should contains the whole children tokens. It may simplify some use case where you may don't have anymore to lookup in handled tokens.
But both approaches are still valid.
Summary by CodeRabbit
New Features
Bug Fixes
Documentation