Skip to content

Commit 8ce2dd9

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

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-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

+14-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{
@@ -2598,6 +2598,7 @@ var _ = Describe("Commands", func() {
25982598
Expect(hGetAll.Err()).NotTo(HaveOccurred())
25992599
Expect(hGetAll.Val()).To(Equal(map[string]string{
26002600
"set1": "val",
2601+
"set6": `{"Number":0}`,
26012602
}))
26022603
var hash3 setOmitEmpty
26032604
Expect(hGetAll.Scan(&hash3)).NotTo(HaveOccurred())
@@ -2606,13 +2607,12 @@ var _ = Describe("Commands", func() {
26062607
Expect(hash3.Set3).To(Equal(time.Duration(0)))
26072608
Expect(hash3.Set4).To(Equal(""))
26082609
Expect(hash3.Set5).To(Equal(time.Time{}))
2609-
Expect(hash3.Set6).To(Equal(childStruct{}))
2610+
Expect(hash3.Set6).To(Equal(numberStruct{}))
26102611

2611-
now := time.Now()
26122612
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
26132613
Set1: "val",
2614-
Set6: childStruct{
2615-
Date: now,
2614+
Set6: &numberStruct{
2615+
Number: 5,
26162616
},
26172617
})
26182618
Expect(hSet.Err()).NotTo(HaveOccurred())
@@ -2622,7 +2622,7 @@ var _ = Describe("Commands", func() {
26222622
Expect(hGetAll.Err()).NotTo(HaveOccurred())
26232623
Expect(hGetAll.Val()).To(Equal(map[string]string{
26242624
"set1": "val",
2625-
"set6": fmt.Sprintf("{\"Date\":\"%s\"}", now.Format(time.RFC3339Nano)),
2625+
"set6": `{"Number":5}`,
26262626
}))
26272627
})
26282628

@@ -7665,12 +7665,16 @@ type numberStruct struct {
76657665
Number int
76667666
}
76677667

7668-
func (s *numberStruct) MarshalBinary() ([]byte, error) {
7669-
return json.Marshal(s)
7668+
func (n *numberStruct) MarshalBinary() ([]byte, error) {
7669+
return json.Marshal(n)
7670+
}
7671+
7672+
func (n *numberStruct) UnmarshalBinary(b []byte) error {
7673+
return json.Unmarshal(b, n)
76707674
}
76717675

7672-
func (s *numberStruct) UnmarshalBinary(b []byte) error {
7673-
return json.Unmarshal(b, s)
7676+
func (n *numberStruct) ScanRedis(str string) error {
7677+
return json.Unmarshal([]byte(str), n)
76747678
}
76757679

76767680
func deref(viface interface{}) interface{} {
@@ -7680,15 +7684,3 @@ func deref(viface interface{}) interface{} {
76807684
}
76817685
return v.Interface()
76827686
}
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)