Skip to content

Commit a52a508

Browse files
authored
Merge pull request redis#1930 from Pyrodash/master
feat: add support for time.Duration write and scan
2 parents 0260525 + a2a463b commit a52a508

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

internal/proto/scan.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ func Scan(b []byte, v interface{}) error {
105105
var err error
106106
*v, err = time.Parse(time.RFC3339Nano, util.BytesToString(b))
107107
return err
108+
case *time.Duration:
109+
n, err := util.ParseInt(b, 10, 64)
110+
if err != nil {
111+
return err
112+
}
113+
*v = time.Duration(n)
114+
return nil
108115
case encoding.BinaryUnmarshaler:
109116
return v.UnmarshalBinary(b)
110117
default:

internal/proto/writer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func (w *Writer) WriteArg(v interface{}) error {
9898
case time.Time:
9999
w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano)
100100
return w.bytes(w.numBuf)
101+
case time.Duration:
102+
return w.int(v.Nanoseconds())
101103
case encoding.BinaryMarshaler:
102104
b, err := v.MarshalBinary()
103105
if err != nil {

redis_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,18 @@ var _ = Describe("Client", func() {
300300
Expect(tm2).To(BeTemporally("==", tm))
301301
})
302302

303+
It("should set and scan durations", func() {
304+
duration := 10 * time.Minute
305+
err := client.Set(ctx, "duration", duration, 0).Err()
306+
Expect(err).NotTo(HaveOccurred())
307+
308+
var duration2 time.Duration
309+
err = client.Get(ctx, "duration").Scan(&duration2)
310+
Expect(err).NotTo(HaveOccurred())
311+
312+
Expect(duration2).To(Equal(duration))
313+
})
314+
303315
It("should Conn", func() {
304316
err := client.Conn(ctx).Get(ctx, "this-key-does-not-exist").Err()
305317
Expect(err).To(Equal(redis.Nil))

0 commit comments

Comments
 (0)