Skip to content

Commit 51dfbae

Browse files
committed
fix: allow hscan to continue past first failure
1 parent 4cb9d14 commit 51dfbae

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

internal/hscan/hscan.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func Struct(dst interface{}) (StructValue, error) {
6767

6868
// Scan scans the results from a key-value Redis map result set to a destination struct.
6969
// The Redis keys are matched to the struct's field with the `redis` tag.
70+
// This method will attempt to unmarshal each field and will return an error if any of the
71+
// fields cannot be unmarshalled. The destination struct will have the failed fields set to
72+
// their zero value.
7073
func Scan(dst interface{}, keys []interface{}, vals []interface{}) error {
7174
if len(keys) != len(vals) {
7275
return errors.New("args should have the same number of keys and vals")
@@ -77,6 +80,8 @@ func Scan(dst interface{}, keys []interface{}, vals []interface{}) error {
7780
return err
7881
}
7982

83+
scanErrors := []error{}
84+
8085
// Iterate through the (key, value) sequence.
8186
for i := 0; i < len(vals); i++ {
8287
key, ok := keys[i].(string)
@@ -90,10 +95,14 @@ func Scan(dst interface{}, keys []interface{}, vals []interface{}) error {
9095
}
9196

9297
if err := strct.Scan(key, val); err != nil {
93-
return err
98+
scanErrors = append(scanErrors, err)
9499
}
95100
}
96101

102+
if len(scanErrors) > 0 {
103+
return fmt.Errorf("scan errors: %v", scanErrors)
104+
}
105+
97106
return nil
98107
}
99108

internal/hscan/hscan_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,21 @@ var _ = Describe("Scan", func() {
175175
Expect(Scan(&d, i{"bool"}, i{""})).To(HaveOccurred())
176176
Expect(Scan(&d, i{"bool"}, i{"123"})).To(HaveOccurred())
177177
})
178+
179+
It("does not stop scanning on first failure", func() {
180+
var d data
181+
182+
keys := i{"bool", "string", "int"}
183+
vals := i{"-1", "foobar", "123"}
184+
185+
err := Scan(&d, keys, vals)
186+
Expect(err).To(HaveOccurred())
187+
188+
Expect(d).To(Equal(data{
189+
Bool: false,
190+
String: "foobar",
191+
Int: 123,
192+
}))
193+
194+
})
178195
})

0 commit comments

Comments
 (0)