Skip to content

Commit 92ad8f3

Browse files
author
Guo Hui
committed
fix: isEmptyValue Struct only support time.Time
1 parent 8860c9e commit 92ad8f3

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

commands.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ func isEmptyValue(v reflect.Value) bool {
156156
case reflect.Interface, reflect.Pointer:
157157
return v.IsNil()
158158
case reflect.Struct:
159-
return v.IsZero()
159+
if v.Type() == reflect.TypeOf(time.Time{}) {
160+
return v.IsZero()
161+
}
162+
// Only supports the struct time.Time,
163+
// subsequent iterations will follow the func Scan support decoder.
160164
}
161165
return false
162166
}

commands_test.go

+16-22
Original file line numberDiff line numberDiff line change
@@ -2585,7 +2585,7 @@ var _ = Describe("Commands", func() {
25852585
Set3 time.Duration `redis:"set3,omitempty"`
25862586
Set4 string `redis:"set4,omitempty"`
25872587
Set5 time.Time `redis:"set5,omitempty"`
2588-
Set6 childStruct `redis:"set6,omitempty"`
2588+
Set6 *numberStruct `redis:"set6,omitempty"`
25892589
}
25902590

25912591
hSet = client.HSet(ctx, "hash3", &setOmitEmpty{
@@ -2606,23 +2606,25 @@ var _ = Describe("Commands", func() {
26062606
Expect(hash3.Set3).To(Equal(time.Duration(0)))
26072607
Expect(hash3.Set4).To(Equal(""))
26082608
Expect(hash3.Set5).To(Equal(time.Time{}))
2609-
Expect(hash3.Set6).To(Equal(childStruct{}))
2609+
Expect(hash3.Set6).To(BeNil())
26102610

26112611
now := time.Now()
26122612
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
26132613
Set1: "val",
2614-
Set6: childStruct{
2615-
Date: now,
2614+
Set5: now,
2615+
Set6: &numberStruct{
2616+
Number: 5,
26162617
},
26172618
})
26182619
Expect(hSet.Err()).NotTo(HaveOccurred())
2619-
Expect(hSet.Val()).To(Equal(int64(2)))
2620+
Expect(hSet.Val()).To(Equal(int64(3)))
26202621

26212622
hGetAll = client.HGetAll(ctx, "hash4")
26222623
Expect(hGetAll.Err()).NotTo(HaveOccurred())
26232624
Expect(hGetAll.Val()).To(Equal(map[string]string{
26242625
"set1": "val",
2625-
"set6": fmt.Sprintf("{\"Date\":\"%s\"}", now.Format(time.RFC3339Nano)),
2626+
"set5": now.Format(time.RFC3339Nano),
2627+
"set6": `{"Number":5}`,
26262628
}))
26272629
})
26282630

@@ -7665,12 +7667,16 @@ type numberStruct struct {
76657667
Number int
76667668
}
76677669

7668-
func (s *numberStruct) MarshalBinary() ([]byte, error) {
7669-
return json.Marshal(s)
7670+
func (n *numberStruct) MarshalBinary() ([]byte, error) {
7671+
return json.Marshal(n)
76707672
}
76717673

7672-
func (s *numberStruct) UnmarshalBinary(b []byte) error {
7673-
return json.Unmarshal(b, s)
7674+
func (n *numberStruct) UnmarshalBinary(b []byte) error {
7675+
return json.Unmarshal(b, n)
7676+
}
7677+
7678+
func (n *numberStruct) ScanRedis(str string) error {
7679+
return json.Unmarshal([]byte(str), n)
76747680
}
76757681

76767682
func deref(viface interface{}) interface{} {
@@ -7680,15 +7686,3 @@ func deref(viface interface{}) interface{} {
76807686
}
76817687
return v.Interface()
76827688
}
7683-
7684-
type childStruct struct {
7685-
Date time.Time `redis:"date,omitempty"`
7686-
}
7687-
7688-
func (c childStruct) MarshalBinary() ([]byte, error) {
7689-
return json.Marshal(&c)
7690-
}
7691-
7692-
func (c childStruct) UnmarshalBinary(data []byte) error {
7693-
return json.Unmarshal(data, &c)
7694-
}

0 commit comments

Comments
 (0)