Skip to content

feat: multi nodes #47

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft

Conversation

haydenhoang
Copy link

Change Summary

work in progress...

PR Checklist

This commit introduces a comprehensive and robust implementation for handling complex document structures within the `#[derive(Typesense)]` macro, enabling powerful schema generation directly from Rust structs.

The macro now supports the full range of advanced indexing strategies offered by Typesense, including automatic object indexing, field flattening with prefix control, and patterns for manual flattening.

### Key Features & Implementation Details

- **Automatic Object Indexing:**
  - A field containing a nested struct that also derives `Document` is now automatically mapped to a Typesense `object` (or `object[]` for `Vec<T>`).
  - This feature requires `#[typesense(enable_nested_fields = true)]` on the parent collection, which the macro now supports.

- **Automatic Field Flattening with `#[typesense(flatten)]`:**
  - A field marked `#[typesense(flatten)]` has its sub-fields expanded into the parent schema using dot-notation.
  - By default, the Rust field's name is used as the prefix for all sub-fields (e.g., `details: ProductDetails` results in schema fields like `details.part_number`).

- **Prefix Override for Flattening:**
  - The `flatten` attribute can be combined with `rename` to provide a custom prefix for the flattened fields.
  - Usage: `#[typesense(flatten, rename = "custom_prefix")]`
  - This provides powerful schema mapping flexibility, allowing the Rust struct's field name to differ from the prefix used in the Typesense schema.

- **Manual Flattening Pattern (`skip` + `rename`):**
  - A new `#[typesense(skip)]` attribute has been introduced to completely exclude a field from the generated Typesense schema.
  - This enables the powerful pattern of sending both nested and flattened data to Typesense: the nested version can be used for display/deserialization, while a separate set of flattened fields is used for indexing. This is achieved by:
    1.  Marking the nested struct field (e.g., `details: Details`) with `#[typesense(skip)]`.
    2.  Adding corresponding top-level fields to the Rust struct, marked with `#[typesense(rename = "details.field_name")]`.

- **Ergonomic Boolean Attributes:**
  - All boolean attributes (`facet`, `sort`, `index`, `store`, `infix`, `stem`, `optional`, `range_index`) now support shorthand "flag" syntax.
  - For example, `#[typesense(sort)]` is a valid and recommended equivalent to `#[typesense(sort = true)]`, dramatically improving readability and consistency.

- **Robust Error Handling & Validation:**
  - The macro provides clear, compile-time errors for invalid or ambiguous attribute usage.
  - It correctly detects and reports duplicate attributes, whether they are in the same `#[typesense(...)]` block or across multiple attributes on the same field.

### Testing

- **Comprehensive Integration Test (`derive_integration.rs`):**
  - A new, full-lifecycle integration test has been added to validate the entire feature set.
  - The test defines a complex struct using every new attribute and pattern, generates a schema, creates a real collection, and uses the generic client (`collection_of<T>`) to perform and validate a full Create, Read, Update, Delete, and Search lifecycle.
  - A second integration test was added to specifically validate the manual flattening pattern.

- **UI Tests:**
  - `trybuild` UI tests have been added to verify that the macro produces the correct compile-time errors for invalid attribute combinations, such as duplicate attributes.
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.

1 participant