Merge pull request #1930 from Pyrodash/master

feat: add support for time.Duration write and scan
This commit is contained in:
Vladimir Mihailenco 2021-10-24 09:54:17 +03:00 committed by GitHub
commit a52a508bbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View File

@ -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:

View File

@ -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 {

View File

@ -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))