Skip to content

Commit

Permalink
Rework ActiveValue (#340)
Browse files Browse the repository at this point in the history
* WIP

* Fixup

* Fixup

* Update docs & rename `unset`

* Deprecate `Unset()` and reexport `ActiveValue::NotSet`

* Docs

Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
  • Loading branch information
billy1624 and tyt2y3 authored Dec 18, 2021
1 parent 5104cd3 commit adfb9ea
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 116 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ Fruit::update_many()
### Save
```rust
let banana = fruit::ActiveModel {
id: Unset(None),
id: NotSet,
name: Set("Banana".to_owned()),
..Default::default()
};

// create, because primary key `id` is `Unset`
// create, because primary key `id` is `NotSet`
let mut banana = banana.save(db).await?.into_active_model();

banana.name = Set("Banana Mongo".to_owned());
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod form {

async fn save_custom_active_model(db: &DbConn) -> Result<(), DbErr> {
let pineapple = form::ActiveModel {
id: Unset(None),
id: NotSet,
name: Set("Pineapple".to_owned()),
};

Expand Down
18 changes: 9 additions & 9 deletions sea-orm-macros/src/derives/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
impl std::convert::From<<Entity as EntityTrait>::Model> for ActiveModel {
fn from(m: <Entity as EntityTrait>::Model) -> Self {
Self {
#(#field: sea_orm::unchanged_active_value_not_intended_for_public_use(m.#field)),*
#(#field: sea_orm::ActiveValue::unchanged(m.#field)),*
}
}
}
Expand All @@ -99,18 +99,18 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> sea_orm::ActiveValue<sea_orm::Value> {
match c {
#(<Self::Entity as EntityTrait>::Column::#name => {
let mut value = sea_orm::ActiveValue::unset();
let mut value = sea_orm::ActiveValue::not_set();
std::mem::swap(&mut value, &mut self.#field);
value.into_wrapped_value()
},)*
_ => sea_orm::ActiveValue::unset(),
_ => sea_orm::ActiveValue::not_set(),
}
}

fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> sea_orm::ActiveValue<sea_orm::Value> {
match c {
#(<Self::Entity as EntityTrait>::Column::#name => self.#field.clone().into_wrapped_value(),)*
_ => sea_orm::ActiveValue::unset(),
_ => sea_orm::ActiveValue::not_set(),
}
}

Expand All @@ -121,23 +121,23 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
}
}

fn unset(&mut self, c: <Self::Entity as EntityTrait>::Column) {
fn not_set(&mut self, c: <Self::Entity as EntityTrait>::Column) {
match c {
#(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::unset(),)*
#(<Self::Entity as EntityTrait>::Column::#name => self.#field = sea_orm::ActiveValue::not_set(),)*
_ => {},
}
}

fn is_unset(&self, c: <Self::Entity as EntityTrait>::Column) -> bool {
fn is_not_set(&self, c: <Self::Entity as EntityTrait>::Column) -> bool {
match c {
#(<Self::Entity as EntityTrait>::Column::#name => self.#field.is_unset(),)*
#(<Self::Entity as EntityTrait>::Column::#name => self.#field.is_not_set(),)*
_ => panic!("This ActiveModel does not have this field"),
}
}

fn default() -> Self {
Self {
#(#field: sea_orm::ActiveValue::unset()),*
#(#field: sea_orm::ActiveValue::not_set()),*
}
}
}
Expand Down
Loading

0 comments on commit adfb9ea

Please sign in to comment.