mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
feat: read the structure to increase the judgment of the omitempty op… (#2529)
* feat: read the structure to increase the judgment of the omitempty option, the same as json Signed-off-by: monkey92t <golang@88.com>
This commit is contained in:
41
commands.go
41
commands.go
@ -106,20 +106,55 @@ func appendStructField(dst []interface{}, v reflect.Value) []interface{} {
|
|||||||
if tag == "" || tag == "-" {
|
if tag == "" || tag == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tag = strings.Split(tag, ",")[0]
|
name, opt, _ := strings.Cut(tag, ",")
|
||||||
if tag == "" {
|
if name == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
field := v.Field(i)
|
field := v.Field(i)
|
||||||
|
|
||||||
|
// miss field
|
||||||
|
if omitEmpty(opt) && isEmptyValue(field) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if field.CanInterface() {
|
if field.CanInterface() {
|
||||||
dst = append(dst, tag, field.Interface())
|
dst = append(dst, name, field.Interface())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func omitEmpty(opt string) bool {
|
||||||
|
for opt != "" {
|
||||||
|
var name string
|
||||||
|
name, opt, _ = strings.Cut(opt, ",")
|
||||||
|
if name == "omitempty" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEmptyValue(v reflect.Value) bool {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||||
|
return v.Len() == 0
|
||||||
|
case reflect.Bool:
|
||||||
|
return !v.Bool()
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return v.Int() == 0
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
return v.Uint() == 0
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return v.Float() == 0
|
||||||
|
case reflect.Interface, reflect.Pointer:
|
||||||
|
return v.IsNil()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type Cmdable interface {
|
type Cmdable interface {
|
||||||
Pipeline() Pipeliner
|
Pipeline() Pipeliner
|
||||||
Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)
|
Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)
|
||||||
|
@ -2136,6 +2136,7 @@ var _ = Describe("Commands", func() {
|
|||||||
Set3 time.Duration `redis:"set3"`
|
Set3 time.Duration `redis:"set3"`
|
||||||
Set4 interface{} `redis:"set4"`
|
Set4 interface{} `redis:"set4"`
|
||||||
Set5 map[string]interface{} `redis:"-"`
|
Set5 map[string]interface{} `redis:"-"`
|
||||||
|
Set6 string `redis:"set6,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
hSet = client.HSet(ctx, "hash", &set{
|
hSet = client.HSet(ctx, "hash", &set{
|
||||||
@ -2148,13 +2149,29 @@ var _ = Describe("Commands", func() {
|
|||||||
Expect(hSet.Err()).NotTo(HaveOccurred())
|
Expect(hSet.Err()).NotTo(HaveOccurred())
|
||||||
Expect(hSet.Val()).To(Equal(int64(4)))
|
Expect(hSet.Val()).To(Equal(int64(4)))
|
||||||
|
|
||||||
hMGet := client.HMGet(ctx, "hash", "set1", "set2", "set3", "set4")
|
hMGet := client.HMGet(ctx, "hash", "set1", "set2", "set3", "set4", "set5", "set6")
|
||||||
Expect(hMGet.Err()).NotTo(HaveOccurred())
|
Expect(hMGet.Err()).NotTo(HaveOccurred())
|
||||||
Expect(hMGet.Val()).To(Equal([]interface{}{
|
Expect(hMGet.Val()).To(Equal([]interface{}{
|
||||||
"val1",
|
"val1",
|
||||||
"1024",
|
"1024",
|
||||||
strconv.Itoa(int(2 * time.Millisecond.Nanoseconds())),
|
strconv.Itoa(int(2 * time.Millisecond.Nanoseconds())),
|
||||||
"",
|
"",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}))
|
||||||
|
|
||||||
|
hSet = client.HSet(ctx, "hash2", &set{
|
||||||
|
Set1: "val2",
|
||||||
|
Set6: "val",
|
||||||
|
})
|
||||||
|
Expect(hSet.Err()).NotTo(HaveOccurred())
|
||||||
|
Expect(hSet.Val()).To(Equal(int64(5)))
|
||||||
|
|
||||||
|
hMGet = client.HMGet(ctx, "hash2", "set1", "set6")
|
||||||
|
Expect(hMGet.Err()).NotTo(HaveOccurred())
|
||||||
|
Expect(hMGet.Val()).To(Equal([]interface{}{
|
||||||
|
"val2",
|
||||||
|
"val",
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user