dojo icon indicating copy to clipboard operation
dojo copied to clipboard

Add write schema 1.5

Open bengineer42 opened this issue 7 months ago • 2 comments

Description

Added the write schema method to dojo to write part of a model efficiently

Related issue

Tests

  • [x] Yes
  • [ ] No, because they aren't needed
  • [ ] No, because I need help

Added to documentation?

  • [ ] README.md
  • [ ] Dojo Book
  • [ ] No documentation needed

Checklist

  • [x] I've formatted my code (scripts/rust_fmt.sh, scripts/cairo_fmt.sh)
  • [ ] I've linted my code (scripts/clippy.sh, scripts/docs.sh)
  • [x] I've commented my code
  • [ ] I've requested a review after addressing the comments

Summary by CodeRabbit

  • New Features

    • Added support for schema-based partial model updates and batch member updates, including new methods for writing and reading schemas.
    • Introduced a new event type for tracking batch member updates.
    • Added new schema and model structures for more flexible data handling.
    • Added a new method to update player configuration items in the example project.
    • Added new configuration file for example project setup.
  • Bug Fixes

    • Improved event handling and serialization for new schema events.
  • Tests

    • Added comprehensive tests for schema-based reading and writing, including equality checks and partial updates.
  • Documentation

    • Updated manifests and configuration files to reflect new features and changes.
  • Style

    • Enhanced code style with additional trait derivations for equality comparisons.
    • Reformatted serialization and deserialization code for improved readability.

bengineer42 avatar Jul 08 '25 09:07 bengineer42

Walkthrough

Ohayo sensei! This update introduces schema-based batch operations for models, new event handling for StoreUpdateMembers, and adds support for partial model writes and reads using schemas. It also extends public traits and enums, updates contract logic, and enhances test coverage for schema operations and equality checks.

Changes

Files/Group Change Summary
bin/sozo/src/commands/events.rs Added handling and formatting for WorldEvent::StoreUpdateMembers in match_event.
crates/dojo/core/src/lib.cairo, crates/dojo/core/src/meta/layout.cairo Changed public exports; added/renamed traits and methods for field layouts and schema support.
crates/dojo/core/src/model/definition.cairo Added Schema variant to ModelIndex enum.
crates/dojo/core/src/model/model.cairo Added to_schemas method to ModelPtrsTrait and its implementation.
crates/dojo/core/src/model/storage.cairo, crates/dojo/core/src/world/storage.cairo Added write_schema/write_schemas methods; updated schema-based read/write logic.
crates/dojo/core/src/world/errors.cairo Added DELETE_ENTITY_SCHEMA error constant.
crates/dojo/core/src/world/world_contract.cairo Added StoreUpdateMembers event struct/variant; extended contract logic for schema batch updates and events.
crates/dojo/world/src/contracts/abigen/world.rs Added StoreUpdateMembers struct/variant; updated serialization, deserialization, and event matching logic.
examples/benchmark/src/model.cairo, crates/dojo/core-tests/src/tests/model/model.cairo Added PartialEq derives, new schema structs, constants, and comprehensive tests for schema operations.
examples/spawn-and-move/manifest_dev.json Updated manifest: new addresses, Schema variant, StoreUpdateMembers event, new contract function.
examples/spawn-and-move/src/actions.cairo Added update_player_config_items method to actions interface and implementation.
examples/spawn-and-move/src/models.cairo Added PlayerConfigItems struct.
examples/spawn-and-move/torii_dev.toml Added new configuration file for Torii indexer.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Contract
    participant Storage

    User->>Contract: set_entity_internal(ModelIndex::Schema, schema_data)
    Contract->>Storage: write_struct_layout(schema_data)
    Contract->>Contract: emit StoreUpdateMembers event (selector, entity_id, member_selectors, values)
sequenceDiagram
    participant User
    participant Contract

    User->>Contract: delete_entity_internal(ModelIndex::Schema)
    Contract->>Contract: panic(DELETE_ENTITY_SCHEMA)

Possibly related PRs

  • dojoengine/dojo#2287: Adds handling and event support for the StoreUpdateMembers event in match_event, closely related to this PR’s event struct and serialization changes.
  • dojoengine/dojo#2649: Modifies the same match_event function in bin/sozo/src/commands/events.rs, focusing on event processing and output formatting, complementing this PR’s event variant addition.
  • dojoengine/dojo#2633: Introduces batch entity operations and the StoreUpdateMembers event struct in the world contract and abigen bindings, sharing core event handling code with this PR.

Suggested labels

contributor

Suggested reviewers

  • glihm
  • kariy

[!NOTE]

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml Review profile: CHILL Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca45360d1fd13c4af779ec1a8fac302d1d379844 and 2eb84d12ef48b044b9dd40f434ca418a5ad78337.

📒 Files selected for processing (3)
  • crates/dojo/world/src/contracts/abigen/model.rs (16 hunks)
  • crates/dojo/world/src/contracts/abigen/world.rs (106 hunks)
  • examples/spawn-and-move/src/actions.cairo (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • examples/spawn-and-move/src/actions.cairo
  • crates/dojo/world/src/contracts/abigen/world.rs
🔇 Additional comments (7)
crates/dojo/world/src/contracts/abigen/model.rs (7)

82-87: Ohayo sensei! LGTM - Clean formatting improvement

The multi-line formatting with trailing commas improves readability in the auto-generated cairo_serialize method.


102-106: Consistent struct construction formatting looks good

The multi-line struct construction with trailing commas maintains consistency with the serialization formatting above.


126-128: Excellent consistent formatting across all serialize methods

All the cairo_serialize method calls have been consistently formatted to use multi-line style with trailing commas. This greatly improves readability and maintainability of the auto-generated code.

Also applies to: 164-166, 207-209, 259-267, 305-310, 348-350


231-237: Well-formatted struct construction in deserialize methods

The struct construction formatting is now consistent with the serialize methods, using multi-line style with trailing commas for better readability.

Also applies to: 281-285, 322-325, 365-369


411-414: Improved error message formatting in TryFrom implementations

The multi-line formatting of the error messages makes them more readable while maintaining the same functionality.

Also applies to: 424-427


505-529: Clean multi-line formatting in Layout enum deserialize method

The consistent multi-line formatting for the cairo_deserialize calls improves code readability while maintaining the same deserialization logic.


614-633: Consistent formatting applied to Ty enum deserialize method

The multi-line formatting matches the pattern used throughout the file, providing better readability for the auto-generated deserialization code.

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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)

  • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

coderabbitai[bot] avatar Jul 08 '25 09:07 coderabbitai[bot]

proc macros have been merged, could you rebase please? Don't hesitate to reach out if you have any questions about the proc macros.

glihm avatar Jul 14 '25 23:07 glihm