From 8a2dfe68b45091ad45cad568d985b8919616b9bc Mon Sep 17 00:00:00 2001 From: Guo Hui Date: Mon, 10 Feb 2025 10:16:04 +0800 Subject: [PATCH 1/3] fix:func isEmptyValue support time.Time --- commands.go | 2 ++ commands_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/commands.go b/commands.go index 034daa235..3e5a4b363 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 829a94b65..7fcc3ba58 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2517,6 +2517,19 @@ var _ = Describe("Commands", func() { "val2", "val", })) + + type set2 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"` + } + hSet = client.HSet(ctx, "hash3", &set2{ + Set1: "val", + }) + Expect(hSet.Err()).NotTo(HaveOccurred()) + Expect(hSet.Val()).To(Equal(int64(1))) }) It("should HSetNX", func() { From 15a6cacd317a283fe779cbeefc5c6f6f293603ff Mon Sep 17 00:00:00 2001 From: Guo Hui Date: Thu, 27 Mar 2025 17:43:19 +0800 Subject: [PATCH 2/3] fix: Improve HSet unit tests --- commands_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index 827a3e213..37ca65105 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2579,18 +2579,26 @@ var _ = Describe("Commands", func() { "val", })) - type set2 struct { + 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"` } - hSet = client.HSet(ctx, "hash3", &set2{ + hSet = client.HSet(ctx, "hash3", &setOmitEmpty{ Set1: "val", }) Expect(hSet.Err()).NotTo(HaveOccurred()) Expect(hSet.Val()).To(Equal(int64(1))) + + var dest setOmitEmpty + hGetAll := client.HGetAll(ctx, "hash3") + Expect(hGetAll.Err()).NotTo(HaveOccurred()) + Expect(hGetAll.Val()).To(Equal(map[string]string{ + "set1": "val", + })) + Expect(hGetAll.Scan(&dest)).NotTo(HaveOccurred()) }) It("should HSetNX", func() { From dbffd845ba47ba21317ddc66caa5f25393f7d354 Mon Sep 17 00:00:00 2001 From: Guo Hui Date: Thu, 3 Apr 2025 18:32:15 +0800 Subject: [PATCH 3/3] feat: Improve HSet unit tests --- commands_test.go | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index 37ca65105..3a332f132 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2585,20 +2585,45 @@ var _ = Describe("Commands", func() { 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))) - var dest setOmitEmpty hGetAll := client.HGetAll(ctx, "hash3") Expect(hGetAll.Err()).NotTo(HaveOccurred()) Expect(hGetAll.Val()).To(Equal(map[string]string{ "set1": "val", })) - Expect(hGetAll.Scan(&dest)).NotTo(HaveOccurred()) + 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() { @@ -7633,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) +}