Skip to content

Commit cde8911

Browse files
authored
Merge branch 'master' into apply-make-fmt
2 parents 9eb0410 + 4bd5d41 commit cde8911

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

commands.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ func isEmptyValue(v reflect.Value) bool {
155155
return v.Float() == 0
156156
case reflect.Interface, reflect.Pointer:
157157
return v.IsNil()
158+
case reflect.Struct:
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.
158164
}
159165
return false
160166
}

commands_test.go

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,63 @@ var _ = Describe("Commands", func() {
25772577
"val2",
25782578
"val",
25792579
}))
2580+
2581+
type setOmitEmpty struct {
2582+
Set1 string `redis:"set1"`
2583+
Set2 int `redis:"set2,omitempty"`
2584+
Set3 time.Duration `redis:"set3,omitempty"`
2585+
Set4 string `redis:"set4,omitempty"`
2586+
Set5 time.Time `redis:"set5,omitempty"`
2587+
Set6 *numberStruct `redis:"set6,omitempty"`
2588+
Set7 numberStruct `redis:"set7,omitempty"`
2589+
}
2590+
2591+
hSet = client.HSet(ctx, "hash3", &setOmitEmpty{
2592+
Set1: "val",
2593+
})
2594+
Expect(hSet.Err()).NotTo(HaveOccurred())
2595+
// both set1 and set7 are set
2596+
// custom struct is not omitted
2597+
Expect(hSet.Val()).To(Equal(int64(2)))
2598+
2599+
hGetAll := client.HGetAll(ctx, "hash3")
2600+
Expect(hGetAll.Err()).NotTo(HaveOccurred())
2601+
Expect(hGetAll.Val()).To(Equal(map[string]string{
2602+
"set1": "val",
2603+
"set7": `{"Number":0}`,
2604+
}))
2605+
var hash3 setOmitEmpty
2606+
Expect(hGetAll.Scan(&hash3)).NotTo(HaveOccurred())
2607+
Expect(hash3.Set1).To(Equal("val"))
2608+
Expect(hash3.Set2).To(Equal(0))
2609+
Expect(hash3.Set3).To(Equal(time.Duration(0)))
2610+
Expect(hash3.Set4).To(Equal(""))
2611+
Expect(hash3.Set5).To(Equal(time.Time{}))
2612+
Expect(hash3.Set6).To(BeNil())
2613+
Expect(hash3.Set7).To(Equal(numberStruct{}))
2614+
2615+
now := time.Now()
2616+
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
2617+
Set1: "val",
2618+
Set5: now,
2619+
Set6: &numberStruct{
2620+
Number: 5,
2621+
},
2622+
Set7: numberStruct{
2623+
Number: 3,
2624+
},
2625+
})
2626+
Expect(hSet.Err()).NotTo(HaveOccurred())
2627+
Expect(hSet.Val()).To(Equal(int64(4)))
2628+
2629+
hGetAll = client.HGetAll(ctx, "hash4")
2630+
Expect(hGetAll.Err()).NotTo(HaveOccurred())
2631+
Expect(hGetAll.Val()).To(Equal(map[string]string{
2632+
"set1": "val",
2633+
"set5": now.Format(time.RFC3339Nano),
2634+
"set6": `{"Number":5}`,
2635+
"set7": `{"Number":3}`,
2636+
}))
25802637
})
25812638

25822639
It("should HSetNX", func() {
@@ -7618,12 +7675,16 @@ type numberStruct struct {
76187675
Number int
76197676
}
76207677

7621-
func (s *numberStruct) MarshalBinary() ([]byte, error) {
7622-
return json.Marshal(s)
7678+
func (n numberStruct) MarshalBinary() ([]byte, error) {
7679+
return json.Marshal(n)
7680+
}
7681+
7682+
func (n *numberStruct) UnmarshalBinary(b []byte) error {
7683+
return json.Unmarshal(b, n)
76237684
}
76247685

7625-
func (s *numberStruct) UnmarshalBinary(b []byte) error {
7626-
return json.Unmarshal(b, s)
7686+
func (n *numberStruct) ScanRedis(str string) error {
7687+
return json.Unmarshal([]byte(str), n)
76277688
}
76287689

76297690
func deref(viface interface{}) interface{} {

0 commit comments

Comments
 (0)