From 2f1b74e20cdd7719b2aecf0768d3e3ae7c3e781b Mon Sep 17 00:00:00 2001 From: Jackie <18378976+Pyrodash@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:26:20 +0200 Subject: [PATCH 1/2] feat: add support for time.Duration write and scan --- internal/proto/scan.go | 7 +++++++ internal/proto/writer.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/internal/proto/scan.go b/internal/proto/scan.go index 7d7183c..596ec44 100644 --- a/internal/proto/scan.go +++ b/internal/proto/scan.go @@ -105,6 +105,13 @@ func Scan(b []byte, v interface{}) error { var err error *v, err = time.Parse(time.RFC3339Nano, util.BytesToString(b)) return err + case *time.Duration: + n, err := util.ParseInt(b, 10, 64) + if err != nil { + return err + } + *v = time.Duration(n) + return nil case encoding.BinaryUnmarshaler: return v.UnmarshalBinary(b) default: diff --git a/internal/proto/writer.go b/internal/proto/writer.go index 81b09b8..c426098 100644 --- a/internal/proto/writer.go +++ b/internal/proto/writer.go @@ -98,6 +98,8 @@ func (w *Writer) WriteArg(v interface{}) error { case time.Time: w.numBuf = v.AppendFormat(w.numBuf[:0], time.RFC3339Nano) return w.bytes(w.numBuf) + case time.Duration: + return w.int(v.Nanoseconds()) case encoding.BinaryMarshaler: b, err := v.MarshalBinary() if err != nil { From a2a463b1237a65bb69cff75beed3ee3bdfae6d4a Mon Sep 17 00:00:00 2001 From: Jackie <18378976+Pyrodash@users.noreply.github.com> Date: Sat, 23 Oct 2021 00:10:30 +0200 Subject: [PATCH 2/2] test: add test case for setting and scanning durations --- redis_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/redis_test.go b/redis_test.go index 05c6d79..095da2d 100644 --- a/redis_test.go +++ b/redis_test.go @@ -300,6 +300,18 @@ var _ = Describe("Client", func() { Expect(tm2).To(BeTemporally("==", tm)) }) + It("should set and scan durations", func() { + duration := 10 * time.Minute + err := client.Set(ctx, "duration", duration, 0).Err() + Expect(err).NotTo(HaveOccurred()) + + var duration2 time.Duration + err = client.Get(ctx, "duration").Scan(&duration2) + Expect(err).NotTo(HaveOccurred()) + + Expect(duration2).To(Equal(duration)) + }) + It("should Conn", func() { err := client.Conn(ctx).Get(ctx, "this-key-does-not-exist").Err() Expect(err).To(Equal(redis.Nil))