Skip to content

Commit 970837b

Browse files
Merge pull request valkey-io#2846 from valkey-io/go-command-hincrbyfloat
GO: Add `HINCRBYFLOAT` command
2 parents e4db1e9 + 8c64329 commit 970837b

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#### Changes
2-
3-
* Go: Add SUNIONSTORE command ([#2805](https://github.com/valkey-io/valkey-glide/pull/2805)
4-
* Go: Add SUNION ([#2787](https://github.com/valkey-io/valkey-glide/pull/2787)
2+
* Go: Add HINCRBYFLOAT command ([#2846](https://github.com/valkey-io/valkey-glide/pull/2846))
3+
* Go: Add SUNIONSTORE command ([#2805](https://github.com/valkey-io/valkey-glide/pull/2805))
4+
* Go: Add SUNION ([#2787](https://github.com/valkey-io/valkey-glide/pull/2787))
55
* Java: bump `netty` version ([#2795](https://github.com/valkey-io/valkey-glide/pull/2795))
66
* Java: Bump protobuf (protoc) version ([#2796](https://github.com/valkey-io/valkey-glide/pull/2796), [#2800](https://github.com/valkey-io/valkey-glide/pull/2800))
77
* Go: Add `SInterStore` ([#2779](https://github.com/valkey-io/valkey-glide/issues/2779))

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) HIncrByFloat(key string, field string, increment float64) (Result[float64], error) {
445+
result, err := client.executeCommand(C.HIncrByFloat, []string{key, field, utils.FloatToString(increment)})
446+
if err != nil {
447+
return CreateNilFloat64Result(), err
448+
}
449+
450+
return handleDoubleResponse(result)
451+
}
452+
444453
func (client *baseClient) LPush(key string, elements []string) (Result[int64], error) {
445454
result, err := client.executeCommand(C.LPush, append([]string{key}, elements...))
446455
if err != nil {

go/api/commands.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,28 @@ type HashCommands interface {
704704
//
705705
// [valkey.io]: https://valkey.io/commands/hstrlen/
706706
HStrLen(key string, field string) (Result[int64], error)
707+
708+
// 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.
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[float64] value of `field` in the hash stored at `key` after the increment.
721+
//
722+
// Example:
723+
// hsetResult, err := client.HSet("key", map[string]string{"field": "10"})
724+
// hincrByFloatResult, err := client.HIncrByFloat("key", "field", 1.5)
725+
// // hincrByFloatResult.Value(): 11.5
726+
//
727+
// [valkey.io]: https://valkey.io/commands/hincrbyfloat/
728+
HIncrByFloat(key string, field string, increment float64) (Result[float64], error)
707729
}
708730

709731
// ConnectionManagementCommands defines an interface for connection management-related commands.

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) TestHIncrByFloat_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+
hincrByFloatResult, hincrByFloatErr := client.HIncrByFloat(key, field, 1.5)
1054+
assert.Nil(suite.T(), hincrByFloatErr)
1055+
assert.Equal(suite.T(), float64(11.5), hincrByFloatResult.Value())
1056+
})
1057+
}
1058+
1059+
func (suite *GlideTestSuite) TestHIncrByFloat_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+
hincrByFloatResult, hincrByFloatErr := client.HIncrByFloat(key, field, 1.5)
1071+
assert.Nil(suite.T(), hincrByFloatErr)
1072+
assert.Equal(suite.T(), float64(1.5), hincrByFloatResult.Value())
1073+
})
1074+
}
1075+
10431076
func (suite *GlideTestSuite) TestLPushLPop_WithExistingKey() {
10441077
suite.runWithDefaultClients(func(client api.BaseClient) {
10451078
list := []string{"value4", "value3", "value2", "value1"}

0 commit comments

Comments
 (0)