Skip to content

Commit 37c057b

Browse files
authored
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 <[email protected]>
1 parent a2aa6b7 commit 37c057b

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

commands.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,55 @@ func appendStructField(dst []interface{}, v reflect.Value) []interface{} {
106106
if tag == "" || tag == "-" {
107107
continue
108108
}
109-
tag = strings.Split(tag, ",")[0]
110-
if tag == "" {
109+
name, opt, _ := strings.Cut(tag, ",")
110+
if name == "" {
111111
continue
112112
}
113113

114114
field := v.Field(i)
115+
116+
// miss field
117+
if omitEmpty(opt) && isEmptyValue(field) {
118+
continue
119+
}
120+
115121
if field.CanInterface() {
116-
dst = append(dst, tag, field.Interface())
122+
dst = append(dst, name, field.Interface())
117123
}
118124
}
119125

120126
return dst
121127
}
122128

129+
func omitEmpty(opt string) bool {
130+
for opt != "" {
131+
var name string
132+
name, opt, _ = strings.Cut(opt, ",")
133+
if name == "omitempty" {
134+
return true
135+
}
136+
}
137+
return false
138+
}
139+
140+
func isEmptyValue(v reflect.Value) bool {
141+
switch v.Kind() {
142+
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
143+
return v.Len() == 0
144+
case reflect.Bool:
145+
return !v.Bool()
146+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
147+
return v.Int() == 0
148+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
149+
return v.Uint() == 0
150+
case reflect.Float32, reflect.Float64:
151+
return v.Float() == 0
152+
case reflect.Interface, reflect.Pointer:
153+
return v.IsNil()
154+
}
155+
return false
156+
}
157+
123158
type Cmdable interface {
124159
Pipeline() Pipeliner
125160
Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error)

commands_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@ var _ = Describe("Commands", func() {
21362136
Set3 time.Duration `redis:"set3"`
21372137
Set4 interface{} `redis:"set4"`
21382138
Set5 map[string]interface{} `redis:"-"`
2139+
Set6 string `redis:"set6,omitempty"`
21392140
}
21402141

21412142
hSet = client.HSet(ctx, "hash", &set{
@@ -2148,13 +2149,29 @@ var _ = Describe("Commands", func() {
21482149
Expect(hSet.Err()).NotTo(HaveOccurred())
21492150
Expect(hSet.Val()).To(Equal(int64(4)))
21502151

2151-
hMGet := client.HMGet(ctx, "hash", "set1", "set2", "set3", "set4")
2152+
hMGet := client.HMGet(ctx, "hash", "set1", "set2", "set3", "set4", "set5", "set6")
21522153
Expect(hMGet.Err()).NotTo(HaveOccurred())
21532154
Expect(hMGet.Val()).To(Equal([]interface{}{
21542155
"val1",
21552156
"1024",
21562157
strconv.Itoa(int(2 * time.Millisecond.Nanoseconds())),
21572158
"",
2159+
nil,
2160+
nil,
2161+
}))
2162+
2163+
hSet = client.HSet(ctx, "hash2", &set{
2164+
Set1: "val2",
2165+
Set6: "val",
2166+
})
2167+
Expect(hSet.Err()).NotTo(HaveOccurred())
2168+
Expect(hSet.Val()).To(Equal(int64(5)))
2169+
2170+
hMGet = client.HMGet(ctx, "hash2", "set1", "set6")
2171+
Expect(hMGet.Err()).NotTo(HaveOccurred())
2172+
Expect(hMGet.Val()).To(Equal([]interface{}{
2173+
"val2",
2174+
"val",
21582175
}))
21592176
})
21602177

0 commit comments

Comments
 (0)