-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Field annotation for interpreting item with user-provided function in AsChangeset #2527
Comments
Can you please clarify what exactly you mean here? Have you any numbers proofing this claim? Otherwise I would say that this theoretical overhead is not measurable in practice, therefore I do not see any reason why this should be changed.
Just to clarify that: Implementing |
Sorry for the ambiguity. Let's say we have a update struct struct A {
...
field: T
...
} I would like to make There do have other workarounds, but it should be more convenient if a conversion function can be added for that field. |
Have you tried to just implement |
What the derive macro does internally is to generate the following code struct A {
...
field: T,
field2: Option<U>,
...
}
impl AsChangeset for A {
fn as_changeset(&self) -> Self::Changeset {
(
...,
table::field.eq(self.field),
self.field2.map(|x| table::field2.eq(x)),
...,
).as_changeset()
}
} Using a user-defined struct will not work as |
That sounds like something that should be fixed in the derive itself. I would accept a PR doing that. (The |
Realted issue: #26
I'd like to build a struct that implements
AsChangeset
, but it is not possible to set some values toNULL
while keeping the struct ignoring someNone
fields. UsingOption<Option<T>>
or another type e.g.enum { Value(T), Null, Unset }
will add extra overhead to the struct and may limit its usability.Instead of adding an extra type, I'd suggest adding a field annotation
#[diesel(parse_with = "parse_func")]
similar to whatserde
use toAsChangeset
, so that users themselves can determine whether to set null or to ignore the field.parse_func
is a function of typeFnMut(T) -> Option<Option<impl AsChangset>>
. This will not break the current behavior and is relatively simple to implement IMHO.e.g.
Let me know if you have any other workarounds other than manually implementing
AsChangeset
. I can work on this when I have some spare time.Edit: The issue is found in
v1.4.x
at the written time. After investigating themaster
branch, it still seems impossible to make a custom value (e.g.serde_json::Value
) returnNULL
.The text was updated successfully, but these errors were encountered: