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

A generic filter for any types and database enums #87

Closed
billy1624 opened this issue Oct 31, 2022 · 1 comment · Fixed by #97
Closed

A generic filter for any types and database enums #87

billy1624 opened this issue Oct 31, 2022 · 1 comment · Fixed by #97
Assignees

Comments

@billy1624
Copy link
Member

Why?

Currently we depends on this list of types to differentiate "built in" type from database enum. I think this is hard to maintain and adding new "built in" type requires changing two places. Adding a derive attribute to TypeFilter and append the new type to the list below.

let default_filters = vec![
"String",
"i8",
"i16",
"i32",
"i64",
"u8",
"u16",
"u32",
"u64",
"f32",
"f64",
#[cfg(feature = "with-chrono")]
"Date",
#[cfg(feature = "with-chrono")]
"DateTime",
#[cfg(feature = "with-chrono")]
"DateTimeUtc",
#[cfg(feature = "with-chrono")]
"DateTimeWithTimeZone",
#[cfg(feature = "with-decimal")]
"Decimal",
#[cfg(feature = "with-json")]
"Json",
#[cfg(feature = "with-uuid")]
"Uuid",
"BinaryVector",
"bool",
];
let filter_item = if default_filters.contains(&type_literal.as_str())
|| type_literal.starts_with("Vec")
{
quote! {
seaography::TypeFilter<#ty>
}
} else {
let ident = format_ident!("{}EnumFilter", type_literal);
quote! {
crate::entities::sea_orm_active_enums::#ident
}
};
quote! {
#ident: Option<#filter_item>
}

Solution?

I think we could introduce a new trait with getter method for all the "built in" types and database enums.

pub struct FilterTrait {
    type Ty:

    fn eq(&self) -> Option<Self::Ty>;
    fn ne(&self) -> Option<Self::Ty>;
    fn gt(&self) -> Option<Self::Ty>;
    fn gte(&self) -> Option<Self::Ty>;
    fn lt(&self) -> Option<Self::Ty>;
    fn lte(&self) -> Option<Self::Ty>;
    fn is_in(&self) -> Option<Vec<Self::Ty>>;
    fn is_not_in(&self) -> Option<Vec<Self::Ty>>;
    fn is_null(&self) -> Option<bool>;
}

Then, we can write this as #ident: Option<FilterTrait<Ty = #type_ident>>

Thoughts? @karatakis

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 31, 2022

Sounds like a brilliant idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants