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

Go: Add IFEQ to SetOptions #3098

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,21 @@ func (client *baseClient) Set(key string, value string) (string, error) {
// Return value:
//
// If the value is successfully set, return api.Result[string] containing "OK".
// If value isn't set because of ConditionalSet.OnlyIfExists or ConditionalSet.OnlyIfDoesNotExist conditions, return
// api.CreateNilStringResult().
// If value isn't set because of ConditionalSet.OnlyIfExists or ConditionalSet.OnlyIfDoesNotExist
// or ConditionalSet.OnlyIfEquals conditions, return api.CreateNilStringResult().
// If SetOptions.returnOldValue is set, return the old value as a String.
//
// For example:
//
// key: initialValue
// result, err := client.SetWithOptions("key", "value", api.NewSetOptionsBuilder()
// .SetExpiry(api.NewExpiryBuilder()
// .SetType(api.Seconds)
// .SetCount(uint64(5)
// ))
// result.Value(): "OK"
// result.IsNil(): false
// key: initialValue
// result, err := client.SetWithOptions("key", "value", api.NewSetOptionsBuilder()
// .SetExpiry(api.NewExpiryBuilder()
// .SetOnlyIfExists()
// .SetType(api.Seconds)
// .SetCount(uint64(5)
// ))
// result.Value(): "OK"
// result.IsNil(): false
//
// [valkey.io]: https://valkey.io/commands/set/
func (client *baseClient) SetWithOptions(key string, value string, options *SetOptions) (Result[string], error) {
Expand Down
48 changes: 48 additions & 0 deletions go/api/command_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type SetOptions struct {
// If ConditionalSet is not set the value will be set regardless of prior value existence. If value isn't set because of
// the condition, [api.StringCommands.SetWithOptions] will return a zero-value string ("").
ConditionalSet ConditionalSet
// Value to compare when [SetOptions.ConditionalSet] is set to `OnlyIfEquals`.
ComparisonValue string
// Set command to return the old value stored at the given key, or a zero-value string ("") if the key did not exist. An
// error is returned and [api.StringCommands.SetWithOptions] is aborted if the value stored at key is not a string.
// Equivalent to GET in the valkey API.
Expand All @@ -32,8 +34,46 @@ func NewSetOptionsBuilder() *SetOptions {
return &SetOptions{}
}

// Sets the condition to [SetOptions.ConditionalSet] for setting the value.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
//
// Deprecated: Use [SetOptions.SetOnlyIfExists], [SetOptions.SetOnlyIfDoesNotExist], or [SetOptions.SetOnlyIfEquals] instead.
func (setOptions *SetOptions) SetConditionalSet(conditionalSet ConditionalSet) *SetOptions {
setOptions.ConditionalSet = conditionalSet
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfExists] for setting the value. The key
// will be set if it already exists.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
func (setOptions *SetOptions) SetOnlyIfExists() *SetOptions {
setOptions.ConditionalSet = OnlyIfExists
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfDoesNotExist] for setting the value. The key
// will not be set if it already exists.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
func (setOptions *SetOptions) SetOnlyIfDoesNotExist() *SetOptions {
setOptions.ConditionalSet = OnlyIfDoesNotExist
setOptions.ComparisonValue = ""
return setOptions
}

// Sets the condition to [SetOptions.OnlyIfEquals] for setting the value. The key
// will be set if the provided comparison value matches the existing value.
//
// This method overrides any previously set [SetOptions.ConditionalSet] and [SetOptions.ComparisonValue].
//
// since Valkey 8.1 and above.
func (setOptions *SetOptions) SetOnlyIfEquals(comparisonValue string) *SetOptions {
setOptions.ConditionalSet = OnlyIfEquals
setOptions.ComparisonValue = comparisonValue
return setOptions
}

Expand All @@ -52,6 +92,9 @@ func (opts *SetOptions) toArgs() ([]string, error) {
var err error
if opts.ConditionalSet != "" {
args = append(args, string(opts.ConditionalSet))
if opts.ConditionalSet == OnlyIfEquals {
args = append(args, opts.ComparisonValue)
}
}

if opts.ReturnOldValue {
Expand Down Expand Up @@ -120,6 +163,11 @@ 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"
// OnlyIfEquals only sets the key if it already exists and the value is equal to the given value. Equivalent to "IFEQ" in
// the valkey API.
//
// since Valkey 8.1 and above.
OnlyIfEquals ConditionalSet = "IFEQ"
)

type ExpireCondition string
Expand Down
32 changes: 28 additions & 4 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfExists_overwrite() {
key := uuid.New().String()
suite.verifyOK(client.Set(key, initialValue))

opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfExists)
opts := api.NewSetOptionsBuilder().SetOnlyIfExists()
result, err := client.SetWithOptions(key, anotherValue, opts)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "OK", result.Value())
Expand All @@ -75,7 +75,7 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfExists_overwrite() {
func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfExists_missingKey() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfExists)
opts := api.NewSetOptionsBuilder().SetOnlyIfExists()
result, err := client.SetWithOptions(key, anotherValue, opts)

assert.Nil(suite.T(), err)
Expand All @@ -86,7 +86,7 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfExists_missingKey() {
func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfDoesNotExist_missingKey() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfDoesNotExist)
opts := api.NewSetOptionsBuilder().SetOnlyIfDoesNotExist()
result, err := client.SetWithOptions(key, anotherValue, opts)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "OK", result.Value())
Expand All @@ -100,7 +100,7 @@ func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfDoesNotExist_missingKey()
func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfDoesNotExist_existingKey() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
opts := api.NewSetOptionsBuilder().SetConditionalSet(api.OnlyIfDoesNotExist)
opts := api.NewSetOptionsBuilder().SetOnlyIfDoesNotExist()
suite.verifyOK(client.Set(key, initialValue))

result, err := client.SetWithOptions(key, anotherValue, opts)
Expand Down Expand Up @@ -174,6 +174,30 @@ func (suite *GlideTestSuite) TestSetWithOptions_UpdateExistingExpiry() {
})
}

func (suite *GlideTestSuite) TestSetWithOptions_OnlyIfEquals() {
suite.SkipIfServerVersionLowerThanBy("8.1.0")
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
suite.verifyOK(client.Set(key, initialValue))

// successful set
opts := api.NewSetOptionsBuilder().SetOnlyIfEquals(initialValue)
result, err := client.SetWithOptions(key, anotherValue, opts)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "OK", result.Value())

result, err = client.Get(key)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), anotherValue, result.Value())

// unsuccessful set
opts = api.NewSetOptionsBuilder().SetOnlyIfEquals(initialValue)
result, err = client.SetWithOptions(key, initialValue, opts)
assert.Nil(suite.T(), err)
assert.True(suite.T(), result.IsNil())
})
}

func (suite *GlideTestSuite) TestGetEx_existingAndNonExistingKeys() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.New().String()
Expand Down
Loading