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: XADD #2843

Merged
merged 7 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "C"

import (
"errors"
"fmt"
"math"
"strconv"
"unsafe"
Expand All @@ -26,6 +27,7 @@ type BaseClient interface {
HashCommands
ListCommands
SetCommands
StreamCommands
ConnectionManagementCommands
GenericBaseCommands
// Close terminates the client by closing all associated resources.
Expand Down Expand Up @@ -1204,3 +1206,23 @@ func (client *baseClient) Renamenx(key string, newKey string) (Result[bool], err
}
return handleBooleanResponse(result)
}

func (client *baseClient) XAdd(key string, values [][]string) (Result[string], error) {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
args := make([]string, 0, 2+2*len(values))
args = append(args, key, "*")
for _, pair := range values {
if len(pair) != 2 {
return CreateNilStringResult(), fmt.Errorf(
"Array entry had the wrong length. Expected length 2 but got length %d",
len(pair),
)
}
args = append(args, pair...)
}

result, err := client.executeCommand(C.XAdd, args)
if err != nil {
return CreateNilStringResult(), err
}
return handleStringResponse(result)
}
29 changes: 29 additions & 0 deletions go/api/stream_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

// Supports commands and transactions for the "Stream Commands" group for standalone and cluster clients.
//
// See [valkey.io] for details.
//
// [valkey.io]: https://valkey.io/commands/#stream
type StreamCommands interface {
// Adds an entry to the specified stream stored at `key`. If the `key` doesn't exist, the stream is created.
//
// See [valkey.io] for details.
//
// Parameters:
// key - The key of the stream.
// values - Field-value pairs to be added to the entry.
//
// Return value:
// The id of the added entry.
//
// For example:
// result, err := client.XAdd("myStream", [][]string{{"field1", "value1"}, {"field2", "value2"}})
// result.IsNil(): false
// result.Value(): "1526919030474-55"
//
// [valkey.io]: https://valkey.io/commands/xadd/
XAdd(key string, values [][]string) (Result[string], error)
}
30 changes: 29 additions & 1 deletion go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3743,7 +3743,7 @@ func (suite *GlideTestSuite) TestUnlink() {
})
}

func (suite *GlideTestSuite) Test_Rename() {
func (suite *GlideTestSuite) TestRename() {
suite.runWithDefaultClients(func(client api.BaseClient) {
// Test 1 Check if the command successfully renamed
key := "{keyName}" + uuid.NewString()
Expand Down Expand Up @@ -3781,3 +3781,31 @@ func (suite *GlideTestSuite) TestRenamenx() {
assert.Equal(suite.T(), false, res2.Value())
})
}

func (suite *GlideTestSuite) TestXAdd() {
suite.runWithDefaultClients(func(client api.BaseClient) {
key := uuid.NewString()
// stream does not exist
res, err := client.XAdd(key, [][]string{{"field1", "value1"}, {"field1", "value2"}})
assert.Nil(suite.T(), err)
assert.False(suite.T(), res.IsNil())
// don't check the value, because it contains server's timestamp

// adding data to existing stream
res, err = client.XAdd(key, [][]string{{"field3", "value3"}})
assert.Nil(suite.T(), err)
assert.False(suite.T(), res.IsNil())

// incorrect input
_, err = client.XAdd(key, [][]string{})
assert.NotNil(suite.T(), err)
_, err = client.XAdd(key, [][]string{{"1", "2", "3"}})
assert.NotNil(suite.T(), err)

// key is not a string
key = uuid.NewString()
client.Set(key, "abc")
_, err = client.XAdd(key, [][]string{{"f", "v"}})
assert.NotNil(suite.T(), err)
})
}
Loading