Skip to content

Commit 9148871

Browse files
committed
修复select或者omit一个空结构体时展示为{},nil slice展示为null
1 parent 49aaacf commit 9148871

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

filter/omit_encode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ TakePointerValue: //取指针的值
9999
}
100100
}
101101
if t.Children == nil && !t.IsAnonymous {
102-
//t.Val = struct{}{} //这样表示返回{}
102+
t.Val = struct{}{} //这样表示返回{}
103103

104-
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
104+
//t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
105105
//说明该结构体上没有选择任何字段 应该返回"字段名:{}"?还是直接连字段名都不显示? 我也不清楚怎么好,后面再说
106106
//反正你啥也不选这字段留着也没任何意义,要就不显示了,至少还能节省一点空间
107107
}
@@ -164,7 +164,7 @@ TakePointerValue: //取指针的值
164164
case reflect.Slice, reflect.Array:
165165
l := valueOf.Len()
166166
if l == 0 {
167-
t.Val = nilSlice //空数组空切片直接解析为[],原生的json解析空的切片和数组会被解析为null,真的很烦,遇到脾气暴躁的前端直接跟你开撕。
167+
t.Val = nil
168168
return
169169
}
170170
t.IsSlice = true

filter/omit_encode_cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ TakePointerValue: //取指针的值
130130
}
131131
}
132132
if t.Children == nil && !t.IsAnonymous {
133-
//t.Val = struct{}{} //这样表示返回{}
133+
t.Val = struct{}{} //这样表示返回{}
134134

135-
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
135+
//t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
136136
//说明该结构体上没有选择任何字段 应该返回"字段名:{}"?还是直接连字段名都不显示? 我也不清楚怎么好,后面再说
137137
//反正你啥也不选这字段留着也没任何意义,要就不显示了,至少还能节省一点空间
138138
}
@@ -195,7 +195,7 @@ TakePointerValue: //取指针的值
195195
case reflect.Slice, reflect.Array:
196196
l := valueOf.Len()
197197
if l == 0 {
198-
t.Val = nilSlice //空数组空切片直接解析为[],原生的json解析空的切片和数组会被解析为null,真的很烦,遇到脾气暴躁的前端直接跟你开撕。
198+
t.Val = nil
199199
return
200200
}
201201
t.IsSlice = true

filter/select_encode.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
)
77

88
var (
9-
nilSlice = make([]int, 0, 0)
109
timeTypes = reflect.TypeOf(time.Now())
1110
)
1211

@@ -99,9 +98,9 @@ TakePointerValue: //取指针的值
9998
}
10099
}
101100
if t.Children == nil && !t.IsAnonymous {
102-
//t.Val = struct{}{} //这样表示返回{}
101+
t.Val = struct{}{} //这样表示返回{}
103102

104-
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
103+
//t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
105104
//说明该结构体上没有选择任何字段 应该返回"字段名:{}"
106105
}
107106
case reflect.Bool,
@@ -163,7 +162,7 @@ TakePointerValue: //取指针的值
163162
case reflect.Slice, reflect.Array:
164163
l := valueOf.Len()
165164
if l == 0 {
166-
t.Val = nilSlice //空数组空切片直接解析为[]
165+
t.Val = nil
167166
return
168167
}
169168
t.IsSlice = true

filter/select_encode_cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ TakePointerValue: //取指针的值
9595
}
9696
}
9797
if t.Children == nil && !t.IsAnonymous {
98-
//t.Val = struct{}{} //这样表示返回{}
98+
t.Val = struct{}{} //这样表示返回{}
9999

100-
t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
100+
//t.IsAnonymous = true //给他搞成匿名字段的处理方式,直接忽略字段
101101
//说明该结构体上没有选择任何字段 应该返回"字段名:{}"
102102
}
103103
case reflect.Bool,
@@ -159,7 +159,7 @@ TakePointerValue: //取指针的值
159159
case reflect.Slice, reflect.Array:
160160
l := valueOf.Len()
161161
if l == 0 {
162-
t.Val = nilSlice //空数组空切片直接解析为[]
162+
t.Val = nil
163163
return
164164
}
165165
t.IsSlice = true

test/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@ func mustJson(v interface{}) string {
1414
return string(marshal)
1515
}
1616

17-
func main() {
17+
type Uu struct {
18+
Name string `json:"name"`
19+
}
1820

19-
fmt.Println(filter.Select("intAll", All{}))
21+
type Us struct {
22+
Name string `json:"name,select(h)"`
23+
H struct{} `json:"h,select(h)"`
24+
Uu Uu `json:"uu,select(h),omit(h)"`
25+
S []string `json:"s,select(h)"`
26+
}
27+
28+
func main() {
29+
//fmt.Println(filter.Select("intAll", All{}))
30+
fmt.Println(filter.Select("h", Us{}))
31+
//fmt.Println(filter.Select("h", struct{}{}))
32+
//fmt.Println(filter.Omit("h", Us{}))
33+
//fmt.Println(filter.Omit("h", struct{}{}))
2034
}

0 commit comments

Comments
 (0)