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

feat(dojo-bindgen): add namespace to unity bindgen #2155

Merged
merged 3 commits into from
Jul 8, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Jul 7, 2024

we use c# namespaces as the top level wrapper for models and SDKs will use reflection to figure out the namespace of the model & reconstruct the tag from that

Summary by CodeRabbit

  • New Features
    • Introduced namespace support for Unity models, improving organization and maintainability of generated code.

@Larkooo Larkooo changed the title feat: add namespace to unity bindgen feat(dojo-bindgen): add namespace to unity bindgen Jul 7, 2024
Copy link

coderabbitai bot commented Jul 7, 2024

Walkthrough

The changes focus on enhancing namespace handling in the format_model function within the unity module of the dojo-bindgen crate. The function now accepts a namespace parameter to wrap the generated classes in a specified namespace. Additionally, get_namespace_from_tag usage is streamlined, improving the organization and readability of generated Unity code.

Changes

File/Path Change Summary
crates/dojo-bindgen/src/plugins/unity/mod.rs Added namespace parameter to format_model and adjusted code formatting. Integrated get_namespace_from_tag.

Poem

In the land where code does bind,
We gave namespaces a place to shine.
With tidier stacks and cleaner tags,
Our Unity models now stand with flags.
In this refined, revamped domain,
CodeRabbit's magic strikes again! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2e04f54 and 2ad234a.

Files selected for processing (1)
  • crates/dojo-bindgen/src/plugins/unity/mod.rs (3 hunks)
Additional comments not posted (4)
crates/dojo-bindgen/src/plugins/unity/mod.rs (4)

6-6: Import statement looks good.

The import of get_namespace_from_tag is necessary for the new functionality.


187-212: Namespace inclusion in format_model function looks good.

The changes correctly wrap the class definition within the provided namespace.


261-261: Usage of get_namespace_from_tag looks good.

The change correctly extracts the namespace from the model tag.


181-181: Function signature change looks good.

The addition of the namespace parameter is necessary for the new functionality.

However, ensure that all function calls to format_model match the new signature.

Verification successful

All function calls to UnityPlugin::format_model match the new signature.

The function signature change has been correctly applied and verified.

  • crates/dojo-bindgen/src/plugins/unity/mod.rs: The call matches the new signature.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `format_model` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type rust $'format_model'

Length of output: 587


Script:

#!/bin/bash
# Description: Verify all function calls to UnityPlugin::format_model match the new signature.

# Test: Search for all calls to UnityPlugin::format_model.
rg --type rust 'UnityPlugin::format_model'

Length of output: 222

Copy link

codecov bot commented Jul 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.99%. Comparing base (617fafb) to head (ac98cb1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2155   +/-   ##
=======================================
  Coverage   67.98%   67.99%           
=======================================
  Files         331      331           
  Lines       42665    42672    +7     
=======================================
+ Hits        29006    29013    +7     
  Misses      13659    13659           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

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

Format and good to go. 👍

Copy link

@coderabbitai coderabbitai bot left a 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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2ad234a and ac98cb1.

Files selected for processing (1)
  • crates/dojo-bindgen/src/plugins/unity/mod.rs (3 hunks)
Additional comments not posted (2)
crates/dojo-bindgen/src/plugins/unity/mod.rs (2)

6-6: Import statement change approved.

The addition of get_namespace_from_tag to the import statement aligns with its usage in the code.


261-265: Ensure get_namespace_from_tag returns a valid namespace.

The usage of the format_model function with the new namespace parameter looks correct. Verify that get_namespace_from_tag returns a valid namespace string.

Verification successful

Ensure get_namespace_from_tag returns a valid namespace.

The get_namespace_from_tag function correctly extracts and returns the namespace part of the tag. The usage of the format_model function with the new namespace parameter looks correct.

  • crates/dojo-world/src/contracts/naming.rs, lines 1-4
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify `get_namespace_from_tag` returns a valid namespace string.

# Test: Search for the definition and usage of `get_namespace_from_tag`. Expect: Valid namespace strings.
rg --type rust $'fn get_namespace_from_tag'

Length of output: 139


Script:

#!/bin/bash
# Extract the implementation of `get_namespace_from_tag` function from the specified file.
ast-grep --lang rust --pattern 'fn get_namespace_from_tag($_) { $$$ }' crates/dojo-world/src/contracts/naming.rs

Length of output: 114


Script:

#!/bin/bash
# Extract the implementation of `get_namespace_from_tag` function from the specified file using ripgrep.
rg -A 20 'fn get_namespace_from_tag' crates/dojo-world/src/contracts/naming.rs

Length of output: 830

Comment on lines +181 to +212
fn format_model(namespace: &str, model: &Composite) -> String {
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
.join("\n\n ");

format!(
"
// Model definition for `{}` model
public class {} : ModelInstance {{
{}

// Start is called before the first frame update
void Start() {{
}}

// Update is called once per frame
void Update() {{
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}

// Start is called before the first frame update
void Start() {{
}}

// Update is called once per frame
void Update() {{
}}
}}
}}

Copy link

Choose a reason for hiding this comment

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

Add validation for the namespace parameter.

The namespace parameter should be validated to ensure it is not empty or null.

fn format_model(namespace: &str, model: &Composite) -> String {
+    assert!(!namespace.is_empty(), "Namespace cannot be empty");
+    assert!(namespace.chars().all(|c| c.is_alphanumeric() || c == '_'), "Namespace can only contain alphanumeric characters and underscores");

    let fields = model
        .inners
        .iter()
        .map(|field| {
            format!(
                "[ModelField(\"{}\")]\n        public {} {};",
                field.name,
                UnityPlugin::map_type(&field.token),
                field.name,
            )
        })
        .collect::<Vec<String>>()
        .join("\n\n        ");

    format!(
        "
namespace {namespace} {{

    // Model definition for `{}` model
    public class {} : ModelInstance {{
        {}

        // Start is called before the first frame update
        void Start() {{
        }}
    
        // Update is called once per frame
        void Update() {{
        }}
    }}
}}

        ",
        model.type_path,
        model.type_name(),
        fields
    )
}
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
fn format_model(namespace: &str, model: &Composite) -> String {
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
.join("\n\n ");
format!(
"
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
}}
}}
}}
fn format_model(namespace: &str, model: &Composite) -> String {
assert!(!namespace.is_empty(), "Namespace cannot be empty");
assert!(namespace.chars().all(|c| c.is_alphanumeric() || c == '_'), "Namespace can only contain alphanumeric characters and underscores");
let fields = model
.inners
.iter()
.map(|field| {
format!(
"[ModelField(\"{}\")]\n public {} {};",
field.name,
UnityPlugin::map_type(&field.token),
field.name,
)
})
.collect::<Vec<String>>()
.join("\n\n ");
format!(
"
namespace {namespace} {{
// Model definition for `{}` model
public class {} : ModelInstance {{
{}
// Start is called before the first frame update
void Start() {{
}}
// Update is called once per frame
void Update() {{
}}
}}
}}
",
model.type_path,
model.type_name(),
fields
)
}

@glihm glihm merged commit f9256ea into dojoengine:main Jul 8, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants