mirror of
https://github.com/redis/go-redis.git
synced 2025-08-08 23:42:06 +03:00
feat(scan): add Scanner interface (#2317)
Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
@@ -30,6 +31,20 @@ type data struct {
|
||||
Bool bool `redis:"bool"`
|
||||
}
|
||||
|
||||
type TimeRFC3339Nano struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t *TimeRFC3339Nano) ScanRedis(s string) (err error) {
|
||||
t.Time, err = time.Parse(time.RFC3339Nano, s)
|
||||
return
|
||||
}
|
||||
|
||||
type TimeData struct {
|
||||
Name string `redis:"name"`
|
||||
Time *TimeRFC3339Nano `redis:"login"`
|
||||
}
|
||||
|
||||
type i []interface{}
|
||||
|
||||
func TestGinkgoSuite(t *testing.T) {
|
||||
@@ -175,4 +190,14 @@ var _ = Describe("Scan", func() {
|
||||
Expect(Scan(&d, i{"bool"}, i{""})).To(HaveOccurred())
|
||||
Expect(Scan(&d, i{"bool"}, i{"123"})).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("Implements Scanner", func() {
|
||||
var td TimeData
|
||||
|
||||
now := time.Now()
|
||||
Expect(Scan(&td, i{"name", "login"}, i{"hello", now.Format(time.RFC3339Nano)})).NotTo(HaveOccurred())
|
||||
Expect(td.Name).To(Equal("hello"))
|
||||
Expect(td.Time.UnixNano()).To(Equal(now.UnixNano()))
|
||||
Expect(td.Time.Format(time.RFC3339Nano)).To(Equal(now.Format(time.RFC3339Nano)))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user