diff --git a/go/api/command_options.go b/go/api/command_options.go index 083fd18b8f..f36e7a1f1c 100644 --- a/go/api/command_options.go +++ b/go/api/command_options.go @@ -42,7 +42,7 @@ func (setOptions *SetOptions) SetReturnOldValue(returnOldValue bool) *SetOptions return setOptions } -func (SetOptions *SetOptions) SetIfeqConditional(comparisonValue string) *SetOptions { +func (SetOptions *SetOptions) SetComparisonValue(comparisonValue string) *SetOptions { SetOptions.ComparisonValue = comparisonValue return SetOptions } @@ -57,8 +57,15 @@ func (opts *SetOptions) toArgs() ([]string, error) { var err error if opts.ConditionalSet != "" { args = append(args, string(opts.ConditionalSet)) - } else if opts.ComparisonValue != "" { + } + + if opts.ConditionalSet == OnlyIfEqual { + if opts.ComparisonValue == "" { + return nil, &RequestError{"comparisonValue must be set when conditionalSet is ONLY_IF_EQUAL"} + } args = append(args, "IFEQ", opts.ComparisonValue) + } else if opts.ComparisonValue != "" { + return nil, &RequestError{"comparisonValue can only be set when conditionalSet is ONLY_IF_EQUAL"} } if opts.ReturnOldValue { @@ -127,6 +134,8 @@ const ( OnlyIfExists ConditionalSet = "XX" // OnlyIfDoesNotExist only sets the key if it does not already exist. Equivalent to "NX" in the valkey API. OnlyIfDoesNotExist ConditionalSet = "NX" + // OnlyIfEqual only sets the key if the current value is equal to the comparisonValue. Equivalent to "IFEQ" in the valkey API. + OnlyIfEqual ConditionalSet = "IFEQ" ) type ExpireCondition string diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index 02fc2fc4c2..6879515449 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -114,6 +114,32 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfDoesNotExist_existingKey() }) } +func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfEQual_CorrectKey() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key := uuid.New().String() + suite.verifyOK(client.Set(key, initialValue)) + // initialValue == current value of key + opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfEqual).SetComparisonValue(initialValue) + result, err := client.SetWithOptions(key, anotherValue, opts) + + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), anotherValue, result.Value()) + }) +} + +func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfEQual_WrongKey() { + suite.runWithDefaultClients(func(client api.BaseClient) { + key := uuid.New().String() + suite.verifyOK(client.Set(key, initialValue)) + // anotherValue != current value of key + opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfEqual).SetComparisonValue(anotherValue) + result, err := client.SetWithOptions(key, anotherValue, opts) + + assert.Nil(suite.T(), err) + assert.Equal(suite.T(), anotherValue, result.Value()) + }) +} + func (suite *GlideTestSuite) TestSetWithOptions_KeepExistingExpiry() { suite.runWithDefaultClients(func(client api.BaseClient) { key := uuid.New().String()