Skip to content

Commit 3b0d10b

Browse files
Support for CLIENT SETINFO (#2659)
* feat: merge master * feat: revert ring.go * feat: add ClientSetInfo command * fix: test and cmd: * fix: test and cmd * fix: test and cmd * fix: test and cmd * feat: redesigning the API * fix: panic test * fix: panic test --------- Co-authored-by: Anuragkillswitch <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent 017466b commit 3b0d10b

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5292,3 +5292,9 @@ func (cmd *ACLLogCmd) readReply(rd *proto.Reader) error {
52925292

52935293
return nil
52945294
}
5295+
5296+
// LibraryInfo holds the library info.
5297+
type LibraryInfo struct {
5298+
LibName *string
5299+
LibVer *string
5300+
}

commands.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ type StatefulCmdable interface {
517517
Select(ctx context.Context, index int) *StatusCmd
518518
SwapDB(ctx context.Context, index1, index2 int) *StatusCmd
519519
ClientSetName(ctx context.Context, name string) *BoolCmd
520+
ClientSetInfo(ctx context.Context, info LibraryInfo) *StatusCmd
520521
Hello(ctx context.Context, ver int, username, password, clientName string) *MapStringInterfaceCmd
521522
}
522523

@@ -574,6 +575,35 @@ func (c statefulCmdable) ClientSetName(ctx context.Context, name string) *BoolCm
574575
return cmd
575576
}
576577

578+
// ClientSetInfo sends a CLIENT SETINFO command with the provided info.
579+
func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *StatusCmd {
580+
err := info.Validate()
581+
if err != nil {
582+
panic(err.Error())
583+
}
584+
585+
var cmd *StatusCmd
586+
if info.LibName != nil {
587+
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", *info.LibName)
588+
} else {
589+
cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer)
590+
}
591+
592+
_ = c(ctx, cmd)
593+
return cmd
594+
}
595+
596+
// Validate checks if only one field in the struct is non-nil.
597+
func (info LibraryInfo) Validate() error {
598+
if info.LibName != nil && info.LibVer != nil {
599+
return errors.New("both LibName and LibVer cannot be set at the same time")
600+
}
601+
if info.LibName == nil && info.LibVer == nil {
602+
return errors.New("at least one of LibName and LibVer should be set")
603+
}
604+
return nil
605+
}
606+
577607
// Hello Set the resp protocol used.
578608
func (c statefulCmdable) Hello(ctx context.Context,
579609
ver int, username, password, clientName string) *MapStringInterfaceCmd {

commands_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,57 @@ var _ = Describe("Commands", func() {
231231
Expect(get.Val()).To(Equal("theclientname"))
232232
})
233233

234+
It("should ClientSetInfo", func() {
235+
236+
pipe := client.Pipeline()
237+
238+
// Test setting the libName
239+
libName := "go-redis"
240+
libInfo := redis.LibraryInfo{LibName: &libName}
241+
setInfo := pipe.ClientSetInfo(ctx, libInfo)
242+
_, err := pipe.Exec(ctx)
243+
244+
Expect(err).NotTo(HaveOccurred())
245+
Expect(setInfo.Err()).NotTo(HaveOccurred())
246+
Expect(setInfo.Val()).To(Equal("OK"))
247+
248+
// Test setting the libVer
249+
libVer := "vX.x"
250+
libInfo = redis.LibraryInfo{LibVer: &libVer}
251+
setInfo = pipe.ClientSetInfo(ctx, libInfo)
252+
_, err = pipe.Exec(ctx)
253+
254+
Expect(err).NotTo(HaveOccurred())
255+
Expect(setInfo.Err()).NotTo(HaveOccurred())
256+
Expect(setInfo.Val()).To(Equal("OK"))
257+
258+
// Test setting both fields, expect a panic
259+
libInfo = redis.LibraryInfo{LibName: &libName, LibVer: &libVer}
260+
261+
Expect(func() {
262+
defer func() {
263+
if r := recover(); r != nil {
264+
err := r.(error)
265+
Expect(err).To(MatchError("both LibName and LibVer cannot be set at the same time"))
266+
}
267+
}()
268+
pipe.ClientSetInfo(ctx, libInfo)
269+
}).To(Panic())
270+
271+
// Test setting neither field, expect a panic
272+
libInfo = redis.LibraryInfo{}
273+
274+
Expect(func() {
275+
defer func() {
276+
if r := recover(); r != nil {
277+
err := r.(error)
278+
Expect(err).To(MatchError("at least one of LibName and LibVer should be set"))
279+
}
280+
}()
281+
pipe.ClientSetInfo(ctx, libInfo)
282+
}).To(Panic())
283+
})
284+
234285
It("should ConfigGet", func() {
235286
val, err := client.ConfigGet(ctx, "*").Result()
236287
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)