Skip to content

Commit

Permalink
adding error hardened scan to mapcmds
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoole committed Oct 21, 2022
1 parent 957d362 commit 8e78e30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
31 changes: 31 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 8e78e30

Please sign in to comment.