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

Add contains method for ActiveValue #1533

Open
negezor opened this issue Mar 10, 2023 · 6 comments · May be fixed by #1614
Open

Add contains method for ActiveValue #1533

negezor opened this issue Mar 10, 2023 · 6 comments · May be fixed by #1614
Assignees

Comments

@negezor
Copy link
Contributor

negezor commented Mar 10, 2023

Motivation

Sometimes it is necessary to compare a value in a model that is already the active model. Now this is done somewhat inconveniently, you need to use !is_not_set() and as_ref(). Which makes the code a bit more complicated.

Proposed Solutions

let id_set = ActiveValue::Set(1);
let id_unchanged = ActiveValue::Unchanged(1);
let id_not_set = ActiveValue::<i32>::Unchanged(1);

id_set.contains(&1); // true
id_unchanged.contains(&1); // true
id_not_set.contains(&1); // false

Current Workarounds

let id_set = ActiveValue::Set(1);

!id_set.is_not_set() && id_set.as_ref() == &1
@prathamsaxen
Copy link

In which file i will find this issue

@negezor
Copy link
Contributor Author

negezor commented Mar 13, 2023

@er-pratham It's supposed to be here
https://github.com/SeaQL/sea-orm/blob/master/src/entity/active_model.rs

@billy1624
Copy link
Member

billy1624 commented Mar 17, 2023

Hey @negezor, interesting. I think implementing PartialEq<T> for ActiveValue might be more conventional?

Such that we can simply write assert!(ActiveValue::Set(1) == 1).

@negezor
Copy link
Contributor Author

negezor commented Mar 17, 2023

Hi @billy1624, I think this behavior is confusing. It sounds like assert!(Option::Some(1) == 1) to me. I think it's worth sticking with how Option does it.

@Diwakar-Gupta
Copy link
Contributor

Diwakar-Gupta commented Apr 16, 2023

@billy1624
Not sure but i did a POC please check

Outcome

    assert!(ActiveValue::Set(2) == 2);
    assert_ne!(ActiveValue::Set(2), 3);
    assert_ne!(
        ActiveValue::Unchanged(String::from("hi")),
        String::from("bye")
    );
    assert!(ActiveValue::Unchanged(String::from("hi")) == String::from("hi"));
impl<V, U> PartialEq<U> for ActiveValue<V>
where
    V: Into<Value> + std::cmp::PartialEq<U>,
    U: Into<Value>,
{
    fn eq(&self, other: &U) -> bool {
        match self {
            Self::Set(value) | Self::Unchanged(value) => value.eq(other),
            Self::NotSet => false,
        }
    }
}

@billy1624
Copy link
Member

Hey @Diwakar-Gupta, the implementation looks promising!! Could you please file a PR for it?

@Diwakar-Gupta Diwakar-Gupta linked a pull request Apr 25, 2023 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage
Development

Successfully merging a pull request may close this issue.

4 participants