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

#[changeset_for] now skips optional fields when None #47

Merged
merged 1 commit into from
Dec 4, 2015

Conversation

sgrif
Copy link
Member

@sgrif sgrif commented Dec 4, 2015

My original thoughts on how to implement this involved boxing and
sticking in a Vec. Unfortunately, that requires implementing
AsChangeset for UserChanges and not &UserChanges, which I don't
want to do by default.

If you actually want to assign NULL, you can still do so by doing
update(table).set(column.eq(None)). In the future we might introduce
an additional type to make it possible to assign null through codegen.

With this change in implementation, I've removed the impl of Changeset
for Vec<T>, as it feels redundant with a tuple containing options.

I've done a best effort to make sure we generate valid SQL in the
various cases. There is still one error case, when the resulting
changeset has 0 fields to update. This will be corrected in another PR,
as I'm still considering whether we should do magic to return the same
thing as we would otherwise, or whether it's an error case.

Fixes #26

@sgrif
Copy link
Member Author

sgrif commented Dec 4, 2015

Review? @mfpiccolo

@sgrif sgrif mentioned this pull request Dec 4, 2015
@mcasper
Copy link
Contributor

mcasper commented Dec 4, 2015

This is great! Good work 👍

@sgrif sgrif force-pushed the sg-optional-changes branch from 787b04b to eec068c Compare December 4, 2015 18:15
My original thoughts on how to implement this involved boxing and
sticking in a `Vec`. Unfortunately, that requires implementing
`AsChangeset` for `UserChanges` and not `&UserChanges`, which I don't
want to do by default.

If you actually want to assign `NULL`, you can still do so by doing
`update(table).set(column.eq(None))`. In the future we might introduce
an additional type to make it possible to assign null through codegen.

With this change in implementation, I've removed the impl of `Changeset`
for `Vec<T>`, as it feels redundant with a tuple containing options.

I've done a best effort to make sure we generate valid SQL in the
various cases. There is still one error case, when the resulting
changeset has 0 fields to update. This will be corrected in another PR,
as I'm still considering whether we should do magic to return the same
thing as we would otherwise, or whether it's an error case.

Fixes #26
@sgrif sgrif force-pushed the sg-optional-changes branch from eec068c to 197e2b8 Compare December 4, 2015 20:52
@mfpiccolo
Copy link
Contributor

Looks great. 👍 Well done!

@sgrif sgrif merged commit 197e2b8 into master Dec 4, 2015
@sgrif sgrif deleted the sg-optional-changes branch December 4, 2015 21:17
sgrif added a commit that referenced this pull request Jan 19, 2016
This allows users to opt into the behavior of `changeset_for` that was
present prior to #47. I still feel that the default behavior of skipping
optional fields is the most common case, as explicitly wanting to set a
column to `NULL` is a pretty niche case to begin with, and much more
frequently the field would be `None` on the struct because you've
populated it from a web form or API endpoint that doesn't specify all
fields.
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 this pull request may close these issues.

3 participants