Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/proto/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"encoding"
"fmt"
"reflect"
"time"

"github.com/go-redis/redis/v8/internal/util"
)

// Scan parses bytes `b` to `v` with appropriate type.
// nolint: gocyclo
func Scan(b []byte, v interface{}) error {
switch v := v.(type) {
case nil:
Expand Down Expand Up @@ -99,6 +102,10 @@ func Scan(b []byte, v interface{}) error {
case *bool:
*v = len(b) == 1 && b[0] == '1'
return nil
case *time.Time:
var err error
*v, err = time.Parse(time.RFC3339Nano, util.BytesToString(b))
return err
case encoding.BinaryUnmarshaler:
return v.UnmarshalBinary(b)
default:
Expand Down
37 changes: 34 additions & 3 deletions internal/proto/scan_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package proto
package proto_test

import (
"context"
"encoding/json"
"errors"
"testing"
"time"

"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/v8/internal/proto"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
Expand All @@ -28,7 +34,7 @@ var _ = Describe("ScanSlice", func() {

It("[]testScanSliceStruct", func() {
var slice []testScanSliceStruct
err := ScanSlice(data, &slice)
err := proto.ScanSlice(data, &slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]testScanSliceStruct{
{-1, "Back Yu"},
Expand All @@ -38,11 +44,36 @@ var _ = Describe("ScanSlice", func() {

It("var testContainer []*testScanSliceStruct", func() {
var slice []*testScanSliceStruct
err := ScanSlice(data, &slice)
err := proto.ScanSlice(data, &slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]*testScanSliceStruct{
{-1, "Back Yu"},
{1, "szyhf"},
}))
})
})

func TestScan(t *testing.T) {
t.Parallel()

t.Run("time", func(t *testing.T) {
t.Parallel()

ctx := context.Background()

rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})

tm := time.Now()
rdb.Set(ctx, "now", tm, 0)

var tm2 time.Time
rdb.Get(ctx, "now").Scan(&tm2)

if !tm2.Equal(tm) {
t.Fatal(errors.New("tm2 and tm are not equal"))
}
})

}