-
-
Notifications
You must be signed in to change notification settings - Fork 530
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
Comments
The idea is to provide a derive macro to implement the necessary traits of a custom wrapper type. For simplicity, we only support 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()
}
} |
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 #[derive(DeriveEntityModel)]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_type = "Text")]
pub name: Option<String>,
} |
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 acceptVec<T>
, where I thinkNullable
can also be implemented.And may be a
column_type
attribute to allow customizing the column type.The text was updated successfully, but these errors were encountered: