Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
69 changes: 65 additions & 4 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 (n *numberStruct) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, n)
}

func (s *numberStruct) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, s)
func (n *numberStruct) ScanRedis(str string) error {
return json.Unmarshal([]byte(str), n)
}

func deref(viface interface{}) interface{} {
Expand Down
Loading