dojo icon indicating copy to clipboard operation
dojo copied to clipboard

fix(core): introduce DojoStore trait to handle some specific storage cases

Open remybar opened this issue 9 months ago • 3 comments
trafficstars

Description

In the current Dojo version, Option<T> and custom enums are not correctly stored in the world storage, because the first variant id is 0 and cannot be differenciated from an uninitialized variant.

To fix that, this PR introduces the DojoStore trait.

  • Every data structure implement the DojoStore trait which, by default, encapsulates Serde calls.
  • The implementation of the DojoStore trait for custom enums and Option<T> handles the variant index incrementing and returns Default::default() as default value during deserialization if the variant index is 0. That means, each custom enums must implement the Default trait.

As these changes may break the world storage, we also introduce the DojoLegacyStorage derive attribute. This attribute must be used on models for which we want to keep the current storage behavior. So, by default, model (de)serialization is done throughDojoStore. If DojoLegacyStorage is set, model (de)serialization is done through Serde.

Tests

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

Added to documentation?

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

Checklist

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

Summary by CodeRabbit

  • New Features

    • Added support for distinguishing between legacy and new storage systems, including querying legacy storage usage in models and contracts.
    • Introduced a new serialization and deserialization trait (DojoStore) for efficient data handling in world storage.
    • Added derive macros and utilities for legacy storage compatibility and enhanced default value handling for enums and structs.
  • Improvements

    • Enhanced model and event macros to support legacy storage with improved diagnostics and generic handling.
    • Updated formatting and printing functions to reflect storage type and improve data representation, especially for enums.
    • Expanded introspection and layout utilities to support conversion between new and legacy storage formats.
    • Refined storage layout validation and error messages for better diagnostics.
  • Bug Fixes

    • Improved error handling and messages for data layout and serialization edge cases.
  • Tests

    • Added extensive tests covering serialization, deserialization, and storage layouts for primitive, composite, generic, and nested data types.
  • Chores

    • Updated dependencies and configuration files to enhance development experience and compatibility.
    • Removed deprecated module and refactored serialization utilities for clarity and maintainability.

remybar avatar Feb 19 '25 10:02 remybar