Skip to content

Commit ef18be3

Browse files
Merge pull request valkey-io#2847 from valkey-io/go-command-hincrby
GO: Add `HINCRBY` command
2 parents 970837b + e61b67c commit ef18be3

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#### Changes
2+
* Go: Add HINCRBY command ([#2847](https://github.com/valkey-io/valkey-glide/pull/2847))
23
* Go: Add HINCRBYFLOAT command ([#2846](https://github.com/valkey-io/valkey-glide/pull/2846))
34
* Go: Add SUNIONSTORE command ([#2805](https://github.com/valkey-io/valkey-glide/pull/2805))
45
* Go: Add SUNION ([#2787](https://github.com/valkey-io/valkey-glide/pull/2787))

go/api/base_client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ func (client *baseClient) HStrLen(key string, field string) (Result[int64], erro
441441
return handleLongResponse(result)
442442
}
443443

444+
func (client *baseClient) HIncrBy(key string, field string, increment int64) (Result[int64], error) {
445+
result, err := client.executeCommand(C.HIncrBy, []string{key, field, utils.IntToString(increment)})
446+
if err != nil {
447+
return CreateNilInt64Result(), err
448+
}
449+
450+
return handleLongResponse(result)
451+
}
452+
444453
func (client *baseClient) HIncrByFloat(key string, field string, increment float64) (Result[float64], error) {
445454
result, err := client.executeCommand(C.HIncrByFloat, []string{key, field, utils.FloatToString(increment)})
446455
if err != nil {

go/api/commands.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,30 @@ type HashCommands interface {
705705
// [valkey.io]: https://valkey.io/commands/hstrlen/
706706
HStrLen(key string, field string) (Result[int64], error)
707707

708+
// Increments the number stored at `field` in the hash stored at `key` by increment.
709+
// By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
710+
// If `field` or `key` does not exist, it is set to 0 before performing the operation.
711+
//
712+
// See [valkey.io] for details.
713+
//
714+
// Parameters:
715+
// key - The key of the hash.
716+
// field - The field in the hash stored at `key` to increment its value.
717+
// increment - The amount to increment.
718+
//
719+
// Return value:
720+
// The Result[int64] value of `field` in the hash stored at `key` after the increment.
721+
//
722+
// Example:
723+
// _, err := client.HSet("key", map[string]string{"field": "10"})
724+
// hincrByResult, err := client.HIncrBy("key", "field", 1)
725+
// // hincrByResult.Value(): 11
726+
//
727+
// [valkey.io]: https://valkey.io/commands/hincrby/
728+
HIncrBy(key string, field string, increment int64) (Result[int64], error)
729+
708730
// Increments the string representing a floating point number stored at `field` in the hash stored at `key` by increment.
709-
// By using a negative increment value, the value stored at field in the hash stored at `key` is decremented.
731+
// By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
710732
// If `field` or `key` does not exist, it is set to 0 before performing the operation.
711733
//
712734
// See [valkey.io] for details.
@@ -720,7 +742,7 @@ type HashCommands interface {
720742
// The Result[float64] value of `field` in the hash stored at `key` after the increment.
721743
//
722744
// Example:
723-
// hsetResult, err := client.HSet("key", map[string]string{"field": "10"})
745+
// _, err := client.HSet("key", map[string]string{"field": "10"})
724746
// hincrByFloatResult, err := client.HIncrByFloat("key", "field", 1.5)
725747
// // hincrByFloatResult.Value(): 11.5
726748
//

go/integTest/shared_commands_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,39 @@ func (suite *GlideTestSuite) TestHStrLen_WithNotExistingField() {
10401040
})
10411041
}
10421042

1043+
func (suite *GlideTestSuite) TestHIncrBy_WithExistingField() {
1044+
suite.runWithDefaultClients(func(client api.BaseClient) {
1045+
key := uuid.NewString()
1046+
field := uuid.NewString()
1047+
fieldValueMap := map[string]string{field: "10"}
1048+
1049+
hsetResult, err := client.HSet(key, fieldValueMap)
1050+
assert.Nil(suite.T(), err)
1051+
assert.Equal(suite.T(), int64(1), hsetResult.Value())
1052+
1053+
hincrByResult, hincrByErr := client.HIncrBy(key, field, 1)
1054+
assert.Nil(suite.T(), hincrByErr)
1055+
assert.Equal(suite.T(), int64(11), hincrByResult.Value())
1056+
})
1057+
}
1058+
1059+
func (suite *GlideTestSuite) TestHIncrBy_WithNonExistingField() {
1060+
suite.runWithDefaultClients(func(client api.BaseClient) {
1061+
key := uuid.NewString()
1062+
field := uuid.NewString()
1063+
field2 := uuid.NewString()
1064+
fieldValueMap := map[string]string{field2: "1"}
1065+
1066+
hsetResult, err := client.HSet(key, fieldValueMap)
1067+
assert.Nil(suite.T(), err)
1068+
assert.Equal(suite.T(), int64(1), hsetResult.Value())
1069+
1070+
hincrByResult, hincrByErr := client.HIncrBy(key, field, 2)
1071+
assert.Nil(suite.T(), hincrByErr)
1072+
assert.Equal(suite.T(), int64(2), hincrByResult.Value())
1073+
})
1074+
}
1075+
10431076
func (suite *GlideTestSuite) TestHIncrByFloat_WithExistingField() {
10441077
suite.runWithDefaultClients(func(client api.BaseClient) {
10451078
key := uuid.NewString()

0 commit comments

Comments
 (0)