diff --git a/commands.go b/commands.go index 6321c15e2..fbd19b290 100644 --- a/commands.go +++ b/commands.go @@ -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 } diff --git a/commands_test.go b/commands_test.go index 55b957496..3a332f132 100644 --- a/commands_test.go +++ b/commands_test.go @@ -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() { @@ -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) +}