Skip to content

Commit

Permalink
add conditionalSetIfEqualTo builder
Browse files Browse the repository at this point in the history
Signed-off-by: Maayan Shani <[email protected]>
  • Loading branch information
Maayanshani25 committed Jan 23, 2025
1 parent ec5cf5c commit 5b73b9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public interface StringBaseCommands {
* }</pre>
* <pre>{@code
* client.set("key", "value").get();
* SetOptions options = SetOptions.builder().conditionalSet(ONLY_IF_EQUAL).comparisonValue("value")).build();
* SetOptions options = SetOptions.builder().conditionalSetIfEqualTo("value").build();
* String value = client.set("key", "newValue", options).get();
* assert value.equals("OK");
* }</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ public enum ConditionalSet {
private final String valkeyApi;
}

// Builder class for SetOptions
public static class SetOptionsBuilder {
private ConditionalSet conditionalSet;
private String comparisonValue;

/**
* Set the condition to ONLY_IF_EQUAL and specify the comparison value
*
* @param value the value to compare
* @return this builder instance
*/
public SetOptionsBuilder conditionalSetIfEqualTo(String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("comparisonValue cannot be null or empty.");
}
this.conditionalSet = ConditionalSet.ONLY_IF_EQUAL;
this.comparisonValue = value;
return this;
}
}

/** Configuration of value lifetime. */
public static final class Expiry {

Expand Down
6 changes: 2 additions & 4 deletions java/client/src/test/java/glide/api/GlideClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,7 @@ public void set_with_SetOptions_OnlyIfEqual_success() {
// Set `key` to `newValue` with the correct condition
SetOptions setOptions =
SetOptions.builder()
.conditionalSet(ONLY_IF_EQUAL)
.comparisonValue(value) // Key must currently have `value`
.conditionalSetIfEqualTo(value) // Key must currently have `value`
.expiry(Expiry.UnixSeconds(60L))
.build();
String[] correctConditionArguments =
Expand Down Expand Up @@ -1023,8 +1022,7 @@ public void set_with_SetOptions_OnlyIfEqual_fails() {
// Attempt to set `key` to `newValue` with the wrong condition
SetOptions wrongConditionOptions =
SetOptions.builder()
.conditionalSet(ONLY_IF_EQUAL)
.comparisonValue(newValue) // Incorrect condition: current value of key is `value`
.conditionalSetIfEqualTo(newValue) // Incorrect condition: current value of key is `value`
.expiry(Expiry.UnixSeconds(60L))
.build();

Expand Down

0 comments on commit 5b73b9f

Please sign in to comment.