1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-19 11:43:14 +03:00

feat: func isEmptyValue support time.Time (#3273)

* fix:func isEmptyValue support time.Time

* fix: Improve HSet unit tests

* feat: Improve HSet unit tests

* fix: isEmptyValue Struct only support time.Time

* test(hset): add empty custom struct test

---------

Co-authored-by: Guo Hui <gh7396@gmail.com>
Co-authored-by: Nedyalko Dyakov <nedyalko.dyakov@gmail.com>
This commit is contained in:
Hui
2025-04-29 05:16:53 +08:00
committed by GitHub
parent 182a04f5fa
commit 4bd5d417ca
2 changed files with 71 additions and 4 deletions

View File

@ -155,6 +155,12 @@ func isEmptyValue(v reflect.Value) bool {
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
case reflect.Struct:
if v.Type() == reflect.TypeOf(time.Time{}) {
return v.IsZero()
}
// Only supports the struct time.Time,
// subsequent iterations will follow the func Scan support decoder.
}
return false
}

View File

@ -2578,6 +2578,63 @@ var _ = Describe("Commands", func() {
"val2",
"val",
}))
type setOmitEmpty struct {
Set1 string `redis:"set1"`
Set2 int `redis:"set2,omitempty"`
Set3 time.Duration `redis:"set3,omitempty"`
Set4 string `redis:"set4,omitempty"`
Set5 time.Time `redis:"set5,omitempty"`
Set6 *numberStruct `redis:"set6,omitempty"`
Set7 numberStruct `redis:"set7,omitempty"`
}
hSet = client.HSet(ctx, "hash3", &setOmitEmpty{
Set1: "val",
})
Expect(hSet.Err()).NotTo(HaveOccurred())
// both set1 and set7 are set
// custom struct is not omitted
Expect(hSet.Val()).To(Equal(int64(2)))
hGetAll := client.HGetAll(ctx, "hash3")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal(map[string]string{
"set1": "val",
"set7": `{"Number":0}`,
}))
var hash3 setOmitEmpty
Expect(hGetAll.Scan(&hash3)).NotTo(HaveOccurred())
Expect(hash3.Set1).To(Equal("val"))
Expect(hash3.Set2).To(Equal(0))
Expect(hash3.Set3).To(Equal(time.Duration(0)))
Expect(hash3.Set4).To(Equal(""))
Expect(hash3.Set5).To(Equal(time.Time{}))
Expect(hash3.Set6).To(BeNil())
Expect(hash3.Set7).To(Equal(numberStruct{}))
now := time.Now()
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
Set1: "val",
Set5: now,
Set6: &numberStruct{
Number: 5,
},
Set7: numberStruct{
Number: 3,
},
})
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(4)))
hGetAll = client.HGetAll(ctx, "hash4")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal(map[string]string{
"set1": "val",
"set5": now.Format(time.RFC3339Nano),
"set6": `{"Number":5}`,
"set7": `{"Number":3}`,
}))
})
It("should HSetNX", func() {
@ -7619,12 +7676,16 @@ type numberStruct struct {
Number int
}
func (s *numberStruct) MarshalBinary() ([]byte, error) {
return json.Marshal(s)
func (n numberStruct) MarshalBinary() ([]byte, error) {
return json.Marshal(n)
}
func (s *numberStruct) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, s)
func (n *numberStruct) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, n)
}
func (n *numberStruct) ScanRedis(str string) error {
return json.Unmarshal([]byte(str), n)
}
func deref(viface interface{}) interface{} {