mirror of
https://github.com/redis/go-redis.git
synced 2025-07-31 05:04:23 +03:00
feat(scan): scan time.Time sets the default decoding (#2413)
* feat(scan): scan time.Time uses `UnmarshalText(RFC3339)` interface decoding by default
This commit is contained in:
@ -200,4 +200,16 @@ var _ = Describe("Scan", func() {
|
||||
Expect(td.Time.UnixNano()).To(Equal(now.UnixNano()))
|
||||
Expect(td.Time.Format(time.RFC3339Nano)).To(Equal(now.Format(time.RFC3339Nano)))
|
||||
})
|
||||
|
||||
It("should time.Time RFC3339Nano", func() {
|
||||
type TimeTime struct {
|
||||
Time time.Time `redis:"time"`
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
var tt TimeTime
|
||||
Expect(Scan(&tt, i{"time"}, i{now.Format(time.RFC3339Nano)})).NotTo(HaveOccurred())
|
||||
Expect(now.Unix()).To(Equal(tt.Time.Unix()))
|
||||
})
|
||||
})
|
||||
|
@ -1,10 +1,13 @@
|
||||
package hscan
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/redis/go-redis/v9/internal/util"
|
||||
)
|
||||
|
||||
// structMap contains the map of struct fields for target structs
|
||||
@ -97,8 +100,11 @@ func (s StructValue) Scan(key string, value string) error {
|
||||
}
|
||||
|
||||
if isPtr && v.Type().NumMethod() > 0 && v.CanInterface() {
|
||||
if scan, ok := v.Interface().(Scanner); ok {
|
||||
switch scan := v.Interface().(type) {
|
||||
case Scanner:
|
||||
return scan.ScanRedis(value)
|
||||
case encoding.TextUnmarshaler:
|
||||
return scan.UnmarshalText(util.StringToBytes(value))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user