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

Sort params are hard to generalize #489

Open
0xpr03 opened this issue Jul 3, 2023 · 1 comment
Open

Sort params are hard to generalize #489

0xpr03 opened this issue Jul 3, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@0xpr03
Copy link

0xpr03 commented Jul 3, 2023

Description
Let's say you have a bunch of indices that all need pagination and sorting.
You could try to use generic structs + serdes flatten and then write a sub function that does all the sort + page query params.
Problem is you need to pass a &[&str] to the sort param. So if you have some kind of sort: Vec<String> then you have to first do sort.iter().map(|v|v.as_ref()).collect(). But this a Vec<&str>. Meaning this has to live for as long as the query exists, thus you can't do something like

fn handle_basics(params: &BaseParams, query: &mut SearchQuery) {
 let c: Vec<_> = params.sort_by.iter().map(|v|v.as_str()).collect();
 query.sort = Some(&c); // needs &[&str]
 // now c would get leaked, creating a lifetime error, c's Vec has to outlive this..
}
fn handle_search(query: BaseParam<Entity>) {
let mut query =...
handle_basics(&params, &mut query);
[..]

Basic example
see above

Other
I'm not sure what the best approach is here. Only solution would be to let SeachQuery own the sort Vec. (sort: Vec<&str>)
Maybe also something like impl Iterator<T> where T: AsRef<str>.

Edit: updated variable names that I forgot to rename correctly from my MVP.

@0xpr03 0xpr03 changed the title Sort params are hard to use Sort params are hard to generalize Jul 3, 2023
@bidoubiwa
Copy link
Contributor

Hey @0xpr03, indeed it's not the best usage :/ Adding an implementation impl Iterator<T> where T: AsRef<str>. is a good option for me to fix this

@bidoubiwa bidoubiwa added the enhancement New feature or request label Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants