Navigation Menu

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

Derive macros for custom wrapper types #1349

Closed
tyt2y3 opened this issue Jan 2, 2023 · 2 comments · Fixed by #1720
Closed

Derive macros for custom wrapper types #1349

tyt2y3 opened this issue Jan 2, 2023 · 2 comments · Fixed by #1720
Milestone

Comments

@tyt2y3
Copy link
Member

tyt2y3 commented Jan 2, 2023

Right now, custom wrapper types kind of work with SeaORM, e.g. 1) https://github.com/SeaQL/sea-orm/blob/master/tests/common/features/event_trigger.rs
2) https://github.com/nitnelave/lldap/pull/382/files#diff-27e26a3b85b610666d7dd0a3fa3e848582ab2d3999f7b1bdfed61052f82af741R96-R168
but require a lot of boilerplate, it'd be nice if we can make a DeriveValueType macro and remove some entropy.

To start, we can only accept struct in the form of struct MyType(pub T). It would be great if we can also accept Vec<T>, where I think Nullable can also be implemented.

And may be a column_type attribute to allow customizing the column type.

@billy1624
Copy link
Member

The idea is to provide a derive macro to implement the necessary traits of a custom wrapper type. For simplicity, we only support struct MyType(T) for now.

Without derive macro, the trait implementations look like below.

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_vec")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub str_vec: StringVec,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringVec(pub Vec<String>);

impl From<StringVec> for Value {
    fn from(source: StringVec) -> Self {
        source.0.into()
    }
}

impl sea_orm::TryGetable for StringVec {
    fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, sea_orm::TryGetError> {
        <Vec<String> as sea_orm::TryGetable>::try_get_by(res, idx).map(|v| StringVec(v))
    }
}

impl sea_query::ValueType for StringVec {
    fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> {
        <Vec<String> as sea_query::ValueType>::try_from(v).map(|v| StringVec(v))
    }

    fn type_name() -> String {
        stringify!(StringVec).to_owned()
    }

    fn array_type() -> sea_orm::sea_query::ArrayType {
        <Vec<String> as sea_orm::sea_query::ValueType>::array_type()
    }

    fn column_type() -> sea_orm::sea_query::ColumnType {
        <Vec<String> as sea_orm::sea_query::ValueType>::column_type()
    }
}

@darkmmon darkmmon mentioned this issue Jun 23, 2023
4 tasks
@tyt2y3
Copy link
Member Author

tyt2y3 commented Jun 27, 2023

Let me share my imagination:

#[derive(DeriveColumnType)]
#[sea_orm(column_type = "String")]
pub struct UserId(Uuid);

// generates
impl ValueType for UserId {
    ...
    fn array_type() -> sea_orm::sea_query::ArrayType {
        ArrayType::String
    }

    fn column_type() -> sea_orm::sea_query::ColumnType {
        ColumnType::String
    }
}

I think column type is needed, when there are multiple possible SQL types a Rust type can be mapped to.

It should pretty much work like DeriveEntityModel's column_type, e.g. https://github.com/SeaQL/sea-orm/blob/master/sea-orm-codegen/tests/compact/cake_with_float.rs

#[derive(DeriveEntityModel)]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    #[sea_orm(column_type = "Text")]
    pub name: Option<String>,
}

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 a pull request may close this issue.

2 participants