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

partialEq for value inside activevalue #1614

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Diwakar-Gupta
Copy link
Contributor

@Diwakar-Gupta Diwakar-Gupta commented Apr 25, 2023

PR Info

New Features

  • this pr enables to compare ActiveValue dosent matter set or unset
assert!(ActiveValue::Set(2) == 2);
assert!(ActiveValue::UnSet(2) == 2);
assert!(ActiveValue::<u8>::NotSet != 2);

Breaking Changes

  • This is backward compatable

Changes

  • added impl<U, V> PartialEq<U> for ActiveValue<V>

@Diwakar-Gupta
Copy link
Contributor Author

Just wanted to inform that

// This will work
assert!(ActiveValue::Set(2) == 2);

// This will give compilation error
assert!(2 == ActiveValue::Set(2));

@billy1624
Copy link
Member

I think this is the best we can get:

assert!(ActiveValue::Set(2) == 2);

We wouldn't be able to make this works:

assert!(2 == ActiveValue::Set(2));

Because we simply can't implement PartialEq<ActiveValue<V>> for U

@billy1624
Copy link
Member

However, I think we can provide an impl<V> PartialEq<sea_query::Value> for ActiveValue<V>?

@Diwakar-Gupta
Copy link
Contributor Author

Diwakar-Gupta commented Apr 26, 2023

PartialEq<sea_query::Value>

This will make sure that we are only comparing with sea_query::Value rather than something that can be converted to sea_query::Value, am i right?

If i am right then user has to convert data to sea_query::Value to compare

ActiveValue::Set(2) == Value::from(2)
// or may be this
ActiveValue::Set(2) == 2.into_value()

@billy1624
Copy link
Member

I mean comparing sea_orm::ActiveValue<?> with sea_query::Value. e.g.

assert!(ActiveValue::Set(2) == Value::from(2));
assert!(ActiveValue::Unchanged(String::from("hi")) == Value::from("hi"));

However, after second thought, it'd be more intuitive to do... instead

assert!(ActiveValue::Set(2) == 2);
assert!(ActiveValue::Unchanged(String::from("hi")) == "hi");

Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!! @Diwakar-Gupta

@billy1624 billy1624 requested a review from tyt2y3 May 3, 2023 03:55
Copy link
Member

@billy1624 billy1624 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Diwakar-Gupta, after second thought... I think comparing ActiveValue<V> with Option<V> make more sense. This way we don't leave out ActiveValue::NotSet.

src/entity/active_model.rs Outdated Show resolved Hide resolved
src/entity/active_model.rs Outdated Show resolved Hide resolved
@tyt2y3
Copy link
Member

tyt2y3 commented May 17, 2023

Hey @Diwakar-Gupta, after second thought... I think comparing ActiveValue<V> with Option<V> make more sense. This way we don't leave out ActiveValue::NotSet.

I still feel like below has a little too much type magic:

assert!(ActiveValue::Set(2) == Some(2));

Actually, the solution can be much simpler:

we only have to add a method:

fn as_ref(&self) -> Option<&V>

Such that we can:

assert!(ActiveValue::Set(2).as_ref() == Some(2).as_ref());

this will work right?

@Diwakar-Gupta
Copy link
Contributor Author

based on the requirement from issue this will work.

Never thought it can be solved with this simplicity.

@Diwakar-Gupta
Copy link
Contributor Author

Can we give thought to possible solution once again

below only utilizes Set and Unchanged only but client api is very simple

assert!(ActiveValue::Set(2) == 2);

this utilizes Set and Unchanged and NotSet

assert!(ActiveValue::Set(2) == Some(2));

and the third one which get's converetd to Option<>

assert!(ActiveValue::Set(2).as_ref() == Some(2).as_ref());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Changes / Comments Requested
Development

Successfully merging this pull request may close these issues.

Add contains method for ActiveValue
3 participants