ActiveValueState::Unchanged

This commit is contained in:
Chris Tsang 2021-05-29 02:02:51 +08:00
parent b1afd5da3e
commit ea091d7438
3 changed files with 28 additions and 3 deletions

View File

@ -41,7 +41,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
impl From<#ident> for ActiveModel {
fn from(m: #ident) -> Self {
Self {
#(#field: sea_orm::ActiveValue::set(m.#field)),*
#(#field: sea_orm::unchanged_active_value_not_intended_for_public_use(m.#field)),*
}
}
}

View File

@ -11,8 +11,9 @@ where
}
#[derive(Clone, Debug)]
pub enum ActiveValueState {
enum ActiveValueState {
Set,
Unchanged,
Unset,
}
@ -22,6 +23,13 @@ impl Default for ActiveValueState {
}
}
pub fn unchanged_active_value_not_intended_for_public_use<V>(value: V) -> ActiveValue<V>
where
V: Default,
{
ActiveValue::unchanged(value)
}
impl<V> ActiveValue<V>
where
V: Default,
@ -37,6 +45,13 @@ where
matches!(self.state, ActiveValueState::Set)
}
pub(crate) fn unchanged(value: V) -> Self {
Self {
value,
state: ActiveValueState::Unchanged,
}
}
pub fn unset() -> Self {
Self {
value: V::default(),
@ -58,6 +73,15 @@ where
}
}
impl<V> std::convert::AsRef<V> for ActiveValue<V>
where
V: Default,
{
fn as_ref(&self) -> &V {
&self.value
}
}
impl<V> ActiveValue<V>
where
V: Default + Into<Value>,
@ -69,6 +93,7 @@ where
pub fn into_wrapped_value(self) -> ActiveValue<Value> {
match self.state {
ActiveValueState::Set => ActiveValue::set(self.into_value()),
ActiveValueState::Unchanged => ActiveValue::set(self.into_value()),
ActiveValueState::Unset => ActiveValue::unset(),
}
}

View File

@ -37,7 +37,7 @@ where
let mut values = Vec::new();
for col in A::Column::iter() {
let av = am.take(col);
if av.is_set() {
if !av.is_unset() {
columns.push(col);
values.push(av.into_value());
}