feat: Extend DeriveIntoActiveModel#2961
Conversation
|
Set as draft until the maintainers decide on the desired design. |
|
But sometimes it makes the semantics ambiguous, e.g. default(false). |
|
By the way, for such a complex case, wouldn’t directly implementing IntoActiveModel be simpler and clearer?” |
Yee, I also think
Regarding the more complex cases: honestly, I’m stuck between a rock and a hard place. Either I write custom implementations for each of the like 50 structs I have, which would roughly double the amount of code or I implement these macros. In the end, macros it is because, in my experience, refactoring many manual implementations is significantly more painful than maintaining macro-based solution. |
|
Because all macros share @tyt2y3, thoughts? |
tyt2y3
left a comment
There was a problem hiding this comment.
I think it looks good and well thought out!
|
should I mark as ready or do you want changes like MetaList, renaming to |
Move field attribute parsing into a function
|
I think it would be better to change variable names and comments once the design and features are finalised. |
|
I think the design is good, only consideration is the name for the |
Rename sets to set_fields
|
Does set accept multiple values? I think we could support a serde-like |
|
Yes, it supports both ways of adding multiple |
kvpavel64
left a comment
There was a problem hiding this comment.
Thanks a ton for your awesome contribution to the community!
PR Info
New Features
DeriveIntoActiveModelActiveModel.ActiveModelfields.Look at the cool table below:
Before this PR
ActiveModelNotSetSet(value)After this PR
ActiveModel#[sea_orm(set(field = "..."))]Set(expr)Option<T>+#[sea_orm(default(expr))]Some(v)->Set(v)None->Set(expr)Set(value)NotSetNotSet/ no effectAlternative solution:
These same features could be implemented as a separate derive macro with its own trait to indicate that the struct is safe to insert.
Showcase:
Computed defaults, constants, and expressions
set(...)supports arbitrary Rust expressions (functions, constants, literals).#[sea_orm(default = "...")]provides fallback expressions forOption<T>fields.set(...)attributes are supported and merged.Exhaustive mode with field control
exhaustiveremoves..Default::default(), forcing explicit coverage of allActiveModelfields.ignorecleanly excludes DTO-only fields.defaultandsetcompose cleanly under exhaustive mode.Breaking Changes
Should be fine, because it's backward compatible and changed code is under
pub(super).Changes
#[sea_orm(default = "...")]to provide fallback values forOption<T>fields whenNone.#[sea_orm(skip)]/#[sea_orm(ignore)]to exclude fields fromIntoActiveModelgeneration.#[sea_orm(set(...))]to explicitly setActiveModelfields with custom expressions.#[sea_orm(active_model = "...")]parsing improvements.IntoActiveModelFieldenum to support normal and default-backed fields.get_as_kv_with_identto support identifier–expression pairs.DeriveIntoActiveModel.