Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: func isEmptyValue support time.Time #3273

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we consider struct as unsupported in

decoders = []decoderFunc{
, should we filter better for time.Time here ? @gh73962

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code will be

    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.

What do u think about this change?

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