Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func isEmptyValue(v reflect.Value) bool {
return v.Float() == 0
case reflect.Interface, reflect.Pointer:
return v.IsNil()
case reflect.Struct:
return v.IsZero()
}
return false
}
Expand Down
58 changes: 58 additions & 0 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,52 @@ 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 childStruct `redis:"set6,omitempty"`
}

hSet = client.HSet(ctx, "hash3", &setOmitEmpty{
Set1: "val",
})
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(1)))

hGetAll := client.HGetAll(ctx, "hash3")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal(map[string]string{
"set1": "val",
}))
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(Equal(childStruct{}))

now := time.Now()
hSet = client.HSet(ctx, "hash4", setOmitEmpty{
Set1: "val",
Set6: childStruct{
Date: now,
},
})
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(2)))

hGetAll = client.HGetAll(ctx, "hash4")
Expect(hGetAll.Err()).NotTo(HaveOccurred())
Expect(hGetAll.Val()).To(Equal(map[string]string{
"set1": "val",
"set6": fmt.Sprintf("{\"Date\":\"%s\"}", now.Format(time.RFC3339Nano)),
}))
})

It("should HSetNX", func() {
Expand Down Expand Up @@ -7612,3 +7658,15 @@ func deref(viface interface{}) interface{} {
}
return v.Interface()
}

type childStruct struct {
Date time.Time `redis:"date,omitempty"`
}

func (c childStruct) MarshalBinary() ([]byte, error) {
return json.Marshal(&c)
}

func (c childStruct) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, &c)
}
Loading