From 8e78e3084b29647268906ecf5de4ca533dc41519 Mon Sep 17 00:00:00 2001 From: Connor Poole Date: Fri, 21 Oct 2022 03:09:31 +0000 Subject: [PATCH] adding error hardened scan to mapcmds --- command.go | 8 +++++++- command_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index a97f9e973..ec224437a 100644 --- a/command.go +++ b/command.go @@ -1206,12 +1206,18 @@ func (cmd *StringStringMapCmd) Scan(dest interface{}) error { return err } + scanErrors := []error{} + for k, v := range cmd.val { if err := strct.Scan(k, v); err != nil { - return err + scanErrors = append(scanErrors, err) } } + if len(scanErrors) > 0 { + return fmt.Errorf("scan errors: %v", scanErrors) + } + return nil } diff --git a/command_test.go b/command_test.go index 168f9f69f..d08665b2e 100644 --- a/command_test.go +++ b/command_test.go @@ -10,6 +10,13 @@ import ( redis "github.com/go-redis/redis/v8" ) +type dataHash struct { + Boolean bool `redis:"boolean"` + String string `redis:"string"` + Uint8 uint8 `redis:"uint8"` + Int int `redis:"int"` +} + var _ = Describe("Cmd", func() { var client *redis.Client @@ -86,6 +93,30 @@ var _ = Describe("Cmd", func() { Expect(tm2).To(BeTemporally("==", tm)) }) + It("does not stop scanning on first failure", func() { + incorrectData := map[string]interface{}{ + "boolean": "-1", //this is the one that causes the error + "string": "foo", + "uint8": "1", + "int": "1", + } + + client.HSet(ctx, "set_key", incorrectData) + + d := dataHash{} + + err := client.HGetAll(ctx, "set_key").Scan(&d) + Expect(err).To(HaveOccurred()) + + Expect(d).To(Equal(dataHash{ + Boolean: false, + String: "foo", + Int: 1, + Uint8: 1, + })) + + }) + It("allows to set custom error", func() { e := errors.New("custom error") cmd := redis.Cmd{}