Skip to content

Commit

Permalink
Go: Add GlideClusterClient examples (valkey-io#3154)
Browse files Browse the repository at this point in the history
Add GlideClusterClient examples

---------

Signed-off-by: Edward Liang <[email protected]>
  • Loading branch information
edlng authored Feb 13, 2025
1 parent 3b206ef commit c8e87f9
Show file tree
Hide file tree
Showing 17 changed files with 2,690 additions and 179 deletions.
61 changes: 37 additions & 24 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,8 @@ func (client *baseClient) SInter(keys []string) (map[string]struct{}, error) {
// Return value:
//
// The number of elements in the resulting set.
//
// [valkey.io]: https://valkey.io/commands/sinterstore/
func (client *baseClient) SInterStore(destination string, keys []string) (int64, error) {
result, err := client.executeCommand(C.SInterStore, append([]string{destination}, keys...))
if err != nil {
Expand Down Expand Up @@ -1695,8 +1697,6 @@ func (client *baseClient) SRandMember(key string) (Result[string], error) {
//
// A Result[string] containing the value of the popped member.
// Returns a NilResult if key does not exist.
//

//
// [valkey.io]: https://valkey.io/commands/spop/
func (client *baseClient) SPop(key string) (Result[string], error) {
Expand Down Expand Up @@ -2553,7 +2553,8 @@ func (client *baseClient) Del(keys []string) (int64, error) {
// the request into sub-requests per slot to ensure atomicity.
//
// Parameters:
// keys - One or more keys to check if they exist.
//
// keys - One or more keys to check if they exist.
//
// Return value:
//
Expand All @@ -2576,8 +2577,9 @@ func (client *baseClient) Exists(keys []string) (int64, error) {
// The timeout will only be cleared by commands that delete or overwrite the contents of key.
//
// Parameters:
// key - The key to expire.
// seconds - Time in seconds for the key to expire
//
// key - The key to expire.
// seconds - Time in seconds for the key to expire
//
// Return value:
//
Expand Down Expand Up @@ -2977,7 +2979,7 @@ func (client *baseClient) PfCount(keys []string) (int64, error) {
//
// Return the number of keys that were unlinked.
//
// [valkey.io]: Https://valkey.io/commands/unlink/
// [valkey.io]: https://valkey.io/commands/unlink/
func (client *baseClient) Unlink(keys []string) (int64, error) {
result, err := client.executeCommand(C.Unlink, keys)
if err != nil {
Expand Down Expand Up @@ -3161,8 +3163,9 @@ func (client *baseClient) XAddWithOptions(
// keysAndIds - A map of keys and entry IDs to read from.
//
// Return value:
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// [valkey.io]: https://valkey.io/commands/xread/
func (client *baseClient) XRead(keysAndIds map[string]string) (map[string]map[string][][]string, error) {
Expand All @@ -3183,8 +3186,9 @@ func (client *baseClient) XRead(keysAndIds map[string]string) (map[string]map[st
// options - Options detailing how to read the stream.
//
// Return value:
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// [valkey.io]: https://valkey.io/commands/xread/
func (client *baseClient) XReadWithOptions(
Expand Down Expand Up @@ -3229,8 +3233,9 @@ func (client *baseClient) XReadWithOptions(
// keysAndIds - A map of keys and entry IDs to read from.
//
// Return value:
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requested entries.
//
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requested entries.
//
// [valkey.io]: https://valkey.io/commands/xreadgroup/
func (client *baseClient) XReadGroup(
Expand All @@ -3257,8 +3262,9 @@ func (client *baseClient) XReadGroup(
// options - Options detailing how to read the stream.
//
// Return value:
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// A `map[string]map[string][][]string` of stream keys to a map of stream entry IDs mapped to an array entries or `nil` if
// a key does not exist or does not contain requiested entries.
//
// [valkey.io]: https://valkey.io/commands/xreadgroup/
func (client *baseClient) XReadGroupWithOptions(
Expand Down Expand Up @@ -4416,6 +4422,7 @@ func (client *baseClient) XPending(key string, group string) (XPendingSummary, e
// opts - The options for the command. See [options.XPendingOptions] for details.
//
// Return value:
//
// A slice of XPendingDetail structs, where each detail struct includes the following fields:
//
// Id - The ID of the pending message.
Expand Down Expand Up @@ -5135,7 +5142,9 @@ func (client *baseClient) XGroupCreateConsumer(
// group - The consumer group name.
// consumer - The consumer to delete.
//
// Returns the number of pending messages the `consumer` had before it was deleted.
// Return value:
//
// The number of pending messages the `consumer` had before it was deleted.
//
// [valkey.io]: https://valkey.io/commands/xgroup-delconsumer/
func (client *baseClient) XGroupDelConsumer(
Expand Down Expand Up @@ -5211,8 +5220,9 @@ func (client *baseClient) SetBit(key string, offset int64, value int64) (int64,
// offset - The index of the bit to return.
//
// Return value:
// The bit at offset of the string. Returns zero if the key is empty or if the positive
// offset exceeds the length of the string.
//
// The bit at offset of the string. Returns zero if the key is empty or if the positive
// offset exceeds the length of the string.
//
// [valkey.io]: https://valkey.io/commands/getbit/
func (client *baseClient) GetBit(key string, offset int64) (int64, error) {
Expand Down Expand Up @@ -5253,8 +5263,9 @@ func (client *baseClient) Wait(numberOfReplicas int64, timeout int64) (int64, er
// key - The key for the string to count the set bits of.
//
// Return value:
// The number of set bits in the string. Returns zero if the key is missing as it is
// treated as an empty string.
//
// The number of set bits in the string. Returns zero if the key is missing as it is
// treated as an empty string.
//
// [valkey.io]: https://valkey.io/commands/bitcount/
func (client *baseClient) BitCount(key string) (int64, error) {
Expand All @@ -5277,8 +5288,9 @@ func (client *baseClient) BitCount(key string) (int64, error) {
// options - The offset options - see [options.BitOffsetOptions].
//
// Return value:
// The number of set bits in the string interval specified by start, end, and options.
// Returns zero if the key is missing as it is treated as an empty string.
//
// The number of set bits in the string interval specified by start, end, and options.
// Returns zero if the key is missing as it is treated as an empty string.
//
// [valkey.io]: https://valkey.io/commands/bitcount/
func (client *baseClient) BitCountWithOptions(key string, opts *options.BitCountOptions) (int64, error) {
Expand Down Expand Up @@ -5787,9 +5799,10 @@ func (client *baseClient) BitFieldRO(key string, commands []options.BitFieldROCo
// Returns the server time.
//
// Return value:
// The current server time as a String array with two elements:
// A UNIX TIME and the amount of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// The current server time as a String array with two elements:
// A UNIX TIME and the amount of microseconds already elapsed in the current second.
// The returned array is in a [UNIX TIME, Microseconds already elapsed] format.
//
// [valkey.io]: https://valkey.io/commands/time/
func (client *baseClient) Time() ([]string, error) {
Expand Down
97 changes: 97 additions & 0 deletions go/api/bitmap_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ func ExampleGlideClient_SetBit() {
// Output: 1
}

func ExampleGlideClusterClient_SetBit() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function

client.SetBit("my_key", 1, 1) // initialize bit 1 with a value of 1

result, err := client.SetBit("my_key", 1, 1) // set bit should return the previous value of bit 1
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: 1
}

func ExampleGlideClient_GetBit() {
var client *GlideClient = getExampleGlideClient() // example helper function

Expand All @@ -35,6 +49,20 @@ func ExampleGlideClient_GetBit() {
// Output: 1
}

func ExampleGlideClusterClient_GetBit() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function

client.SetBit("my_key", 1, 1)
client.SetBit("my_key", 1, 1)
result, err := client.GetBit("my_key", 1)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: 1
}

func ExampleGlideClient_BitCount() {
var client *GlideClient = getExampleGlideClient() // example helper function

Expand All @@ -49,6 +77,20 @@ func ExampleGlideClient_BitCount() {
// Output: 2
}

func ExampleGlideClusterClient_BitCount() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function

client.SetBit("my_key", 1, 1)
client.SetBit("my_key", 2, 1)
result, err := client.BitCount("my_key")
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: 2
}

func ExampleGlideClient_BitCountWithOptions() {
var client *GlideClient = getExampleGlideClient() // example helper function

Expand All @@ -65,6 +107,22 @@ func ExampleGlideClient_BitCountWithOptions() {
// Output: 0
}

func ExampleGlideClusterClient_BitCountWithOptions() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function

options := options.NewBitCountOptionsBuilder().
SetStart(1).
SetEnd(1).
SetBitmapIndexType(options.BYTE)
result, err := client.BitCountWithOptions("my_key", options)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: 0
}

func ExampleGlideClient_BitField() {
var client *GlideClient = getExampleGlideClient() // example helper function

Expand All @@ -83,6 +141,24 @@ func ExampleGlideClient_BitField() {
// output: [{0 false} {0 false} {1 false}]
}

func ExampleGlideClusterClient_BitField() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function

commands := []options.BitFieldSubCommands{
options.NewBitFieldGet(options.SignedInt, 8, 16),
options.NewBitFieldOverflow(options.SAT),
options.NewBitFieldSet(options.UnsignedInt, 4, 0, 7),
options.NewBitFieldIncrBy(options.SignedInt, 5, 100, 1),
}
result, err := client.BitField("mykey", commands)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// output: [{0 false} {0 false} {1 false}]
}

func ExampleGlideClient_BitFieldRO() {
var client *GlideClient = getExampleGlideClient() // example helper function
key := "mykey"
Expand All @@ -103,3 +179,24 @@ func ExampleGlideClient_BitFieldRO() {

// output: [{24 false}]
}

func ExampleGlideClusterClient_BitFieldRO() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
key := "mykey"

bfcommands := []options.BitFieldSubCommands{
options.NewBitFieldSet(options.UnsignedInt, 8, 0, 24),
}
client.BitField(key, bfcommands)

commands := []options.BitFieldROCommands{
options.NewBitFieldGet(options.UnsignedInt, 8, 0),
}
result, err := client.BitFieldRO(key, commands)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// output: [{24 false}]
}
2 changes: 2 additions & 0 deletions go/api/connection_management_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ type ConnectionManagementClusterCommands interface {

PingWithOptions(pingOptions options.ClusterPingOptions) (string, error)

Echo(message string) (Result[string], error)

EchoWithOptions(echoOptions options.ClusterEchoOptions) (ClusterValue[string], error)
}
65 changes: 65 additions & 0 deletions go/api/connection_management_cluster_commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

package api

import (
"fmt"

"github.com/valkey-io/valkey-glide/go/api/options"
)

func ExampleGlideClusterClient_Ping() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
result, err := client.Ping()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: PONG
}

func ExampleGlideClusterClient_PingWithOptions() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
options := options.ClusterPingOptions{
PingOptions: &options.PingOptions{
Message: "hello",
},
Route: nil,
}
result, err := client.PingWithOptions(options)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: hello
}

func ExampleGlideClusterClient_Echo() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
result, err := client.Echo("Hello")
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result)

// Output: {Hello false}
}

func ExampleGlideClusterClient_EchoWithOptions() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
opts := options.ClusterEchoOptions{
EchoOptions: &options.EchoOptions{
Message: "Hello World",
},
RouteOption: &options.RouteOption{Route: nil},
}
result, err := client.EchoWithOptions(opts)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(result.singleValue)

// Output: Hello World
}
Loading

0 comments on commit c8e87f9

Please sign in to comment.