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

Implemented IFEQ set command in python with tests #2962

Merged
merged 4 commits into from
Jan 21, 2025

Conversation

Angraybill
Copy link
Collaborator

@Angraybill Angraybill commented Jan 16, 2025

Implements the IFEQ option into the SET command. Tests were added on test_async_client.py.

Issue link

This Pull Request is linked to issue (URL): #2811

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one issue.
  • Commit message has a detailed description of what changed and why.
  • Tests are added or updated.
  • CHANGELOG.md and documentation files are updated.
  • Destination branch is correct - main or release
  • Create merge commit if merging release branch into main, squash otherwise.

@Angraybill Angraybill requested a review from a team as a code owner January 16, 2025 19:09
@Yury-Fridlyand Yury-Fridlyand added the python Python wrapper label Jan 16, 2025
@Angraybill
Copy link
Collaborator Author

I'm open to suggestions, critique, and potential variable name changes.

@@ -73,10 +73,12 @@ class ConditionalChange(Enum):
A condition to the `SET`, `ZADD` and `GEOADD` commands.
- ONLY_IF_EXISTS - Only update key / elements that already exist. Equivalent to `XX` in the Valkey API.
- ONLY_IF_DOES_NOT_EXIST - Only set key / add elements that does not already exist. Equivalent to `NX` in the Valkey API.
- ONLY_IF_EQUAL - Only update key if the provided value is equal to the old value. Equivalent to `IFEQ` in the Valkey API.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bit of a problem, since this type is optional in ZADD and GEOADD as well, and we can't let IFEQ part of it.
We need to plan it a bit different, but I want to make sure first if there are any plans to add it to the other in the future so we can take a smart decision of how to handle it.
Let's keep working on the PR, but keep in mind that we are going to make changes here, it will need to be a separate type. Ill tag on the discussion in Valkey WS, and we will discuss it offline to understand it better.

In addition, while the others are not requiring extra parameter, IFEQ, and we do not want to allow users mistakenly adding one of the parameters and the other and fail on run time, and we want to enforce to the best we can do, the right usage of the type, more like we do in the ExpiryType than as we do it in ConditionalChange, but let's wait for response from community, and then lets plan it better.

Copy link
Collaborator

Choose a reason for hiding this comment

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

To whom come across - the solution we decided to go on is a Union between ConditionalChange and a new class OnlyIfEqual which will include comparison_value as a field.

The reason not to create a separate SetConditionalChange Enum to use a Union between the two, is that Enum doesn't enforce using comparison_value along with OnlyIfEqual and will end up as a runtime error, while can be avoided with type checking when it is a class.
The reason that the class is OnlyIfEqual and not more generic is because to make it more generic we need to avoid setting specific required fields, and that's miss the logic in trying to enforce comparison_value.

@avifenesh avifenesh added the Feature Additional feature, big or small label Jan 17, 2025
@avifenesh avifenesh added this to the 1.3 milestone Jan 17, 2025
@Angraybill
Copy link
Collaborator Author

It would appear that I added one commit too many to the PR. It won't change anything (it was just my typo fixes from my last PR) but it's a little redundant.


elif isinstance(conditional_set, OnlyIfEqual):
args.append("IFEQ")
if conditional_set.comparison_value:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we added the parameter to be a Class with a field this if is not relevant anymore, the class has to have this field.

@dataclass
class OnlyIfEqual():
"""
Condition to the `SET` command
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Condition to the `SET` command
Change condition to the `SET` command,
For additional conditonal options see ConditionalChange

@avifenesh
Copy link
Collaborator

Test fails since you created a new type, hence needs to be imported to https://github.com/valkey-io/valkey-glide/blob/main/python/python/glide/__init__.py to be available for users.

@avifenesh
Copy link
Collaborator

Please fix linting failures

@Angraybill
Copy link
Collaborator Author

The formatter reformatted the way I call the SET command to a way that isn't consistent with the rest of the test files.

@avifenesh
Copy link
Collaborator

The formatter reformatted the way I call the SET command to a way that isn't consistent with the rest of the test files.

Please give more details

@Angraybill
Copy link
Collaborator Author

All of the times SET is called, they format it like
glide_client.set( "key", "value", )
But the formatter redid it so it looks like
glide_client.set( "key" "value )

@Angraybill
Copy link
Collaborator Author

ok GitHub did not format that the way I hoped. Basically the formatter put new lines between every parameter despite that not happening in the rest of the test cases

@avifenesh avifenesh removed this from the 1.3 milestone Jan 21, 2025
Copy link
Collaborator

@avifenesh avifenesh left a comment

Choose a reason for hiding this comment

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

Great work! Even after the surprising challenges, it looks good, clean code, good docs, and extra fixes. Approved.

@Angraybill Angraybill merged commit e29f6d3 into valkey-io:main Jan 21, 2025
22 checks passed
Maayanshani25 pushed a commit to Maayanshani25/valkey-glide that referenced this pull request Jan 22, 2025
* Define new function, change paramter type to Union, add conditionals for IFEQ

Signed-off-by: Angraybill <[email protected]>

* Tests for IFEQ, positive and negative cases

Signed-off-by: Angraybill <[email protected]>

* Update Changelog

Signed-off-by: Angraybill <[email protected]>

* Import OnlyIfEqual in init file

Signed-off-by: Angraybill <[email protected]>

---------

Signed-off-by: Angraybill <[email protected]>
EdricCua pushed a commit to EdricCua/valkey-glide that referenced this pull request Jan 23, 2025
* Define new function, change paramter type to Union, add conditionals for IFEQ

Signed-off-by: Angraybill <[email protected]>

* Tests for IFEQ, positive and negative cases

Signed-off-by: Angraybill <[email protected]>

* Update Changelog

Signed-off-by: Angraybill <[email protected]>

* Import OnlyIfEqual in init file

Signed-off-by: Angraybill <[email protected]>

---------

Signed-off-by: Angraybill <[email protected]>
EdricCua pushed a commit to EdricCua/valkey-glide that referenced this pull request Jan 23, 2025
* Define new function, change paramter type to Union, add conditionals for IFEQ

Signed-off-by: Angraybill <[email protected]>

* Tests for IFEQ, positive and negative cases

Signed-off-by: Angraybill <[email protected]>

* Update Changelog

Signed-off-by: Angraybill <[email protected]>

* Import OnlyIfEqual in init file

Signed-off-by: Angraybill <[email protected]>

---------

Signed-off-by: Angraybill <[email protected]>
eifrah-aws pushed a commit to eifrah-aws/glide-for-redis that referenced this pull request Jan 23, 2025
* Define new function, change paramter type to Union, add conditionals for IFEQ

Signed-off-by: Angraybill <[email protected]>

* Tests for IFEQ, positive and negative cases

Signed-off-by: Angraybill <[email protected]>

* Update Changelog

Signed-off-by: Angraybill <[email protected]>

* Import OnlyIfEqual in init file

Signed-off-by: Angraybill <[email protected]>

---------

Signed-off-by: Angraybill <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Additional feature, big or small python Python wrapper
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants