Skip to content

Feat: use inner <T as ToTypesenseField>::to_typesense_type() of Vec<T> in derive#85

Merged
RoDmitry merged 2 commits into
typesense:mainfrom
milesgranger:milesgranger/vec-of-local-type-support
May 6, 2026
Merged

Feat: use inner <T as ToTypesenseField>::to_typesense_type() of Vec<T> in derive#85
RoDmitry merged 2 commits into
typesense:mainfrom
milesgranger:milesgranger/vec-of-local-type-support

Conversation

@milesgranger
Copy link
Copy Markdown
Contributor

@milesgranger milesgranger commented May 6, 2026

Change Summary

Deriving Typesense on a struct with a Vec<MyLocalType> field only compiles if MyLocalType: Document (giving "object[]") or is a built-in primitive. And we cannot do impl ToTypesenseField for Vec<MyLocalType> in downstream crates, so requires the #[typesense(type = "...")] per-field work around.

This PR mirrors what build_regular_field already does for flatten: peel Vec<_> (and Vec<Option<_>>) in the proc-macro, resolve ToTypesenseField on the inner type, and append "[]". Downstream crates now only need MyLocalType: ToTypesenseField.

Example that compiles after this PR but not before:

 use serde::{Serialize, Deserialize};                                      
 use typesense::{Typesense, prelude::ToTypesenseField};  
                                                                           
 #[derive(Serialize, Deserialize)]                       
 pub enum Priority { Low, Medium, High }                                   
                                                                           
 impl ToTypesenseField for Priority {
     fn to_typesense_type() -> &'static str { "string" }                   
 }                                                                         

 #[derive(Typesense, Serialize, Deserialize)]                              
 struct Task {                                           
     priority: Priority,        // "string"      (worked before)           
     status: Option<Priority>,  // "string"      (worked before)
     tags: Vec<Priority>,       // "string[]"    (NEW: required #[typesense(type = "string[]")] before)                                   
 }                                                                         

PR Checklist

@RoDmitry RoDmitry self-requested a review May 6, 2026 12:57
@RoDmitry
Copy link
Copy Markdown
Collaborator

RoDmitry commented May 6, 2026

Nice solution 👍
I have removed extra ToTypesenseField implementations, and added tests. Tell me if it looks good, and check if it compiles for your code.

@RoDmitry RoDmitry force-pushed the milesgranger/vec-of-local-type-support branch from 7fabd17 to e9fbcc7 Compare May 6, 2026 13:26
@milesgranger
Copy link
Copy Markdown
Contributor Author

Super! Yes, looks good and things are still happy on my side. :)
Thank you!

@RoDmitry RoDmitry changed the title Resolve ToTypesenseField on element type for Vec<_> fields Feat: use inner <T as ToTypesenseField>::to_typesense_type() of Vec<T> in derive May 6, 2026
@RoDmitry RoDmitry merged commit 6a55007 into typesense:main May 6, 2026
3 checks passed
@milesgranger milesgranger deleted the milesgranger/vec-of-local-type-support branch May 6, 2026 17:22
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