Skip to content

Commit d9efa6a

Browse files
author
beiluo
committed
fix: 修复omit时候未导出字段 panic的问题
1 parent 1482cfe commit d9efa6a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

filter/parser.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type tagInfo struct {
1313

1414
func (t *fieldNodeTree) parseAny(key, scene string, valueOf reflect.Value, isSelect bool) {
1515
typeOf := valueOf.Type()
16+
1617
TakePointerValue: //取指针的值
1718
switch typeOf.Kind() {
1819
case reflect.Ptr: //如果是指针类型则取值重新判断类型
@@ -215,7 +216,9 @@ func parserStruct(typeOf reflect.Type, valueOf reflect.Value, t *fieldNodeTree,
215216
}
216217
pkgInfo := typeOf.PkgPath() + "." + typeOf.Name()
217218
for i := 0; i < typeOf.NumField(); i++ {
218-
219+
if !typeOf.Field(i).IsExported() { //跳过非导出字段
220+
continue
221+
}
219222
var tagInfo tagInfo
220223
tagInfo = getSelectTag(scene, pkgInfo, i, typeOf)
221224
if !isSelect {

test/main.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,23 @@ func goSelect() {
8686

8787
}
8888

89+
type User struct {
90+
user string
91+
age int
92+
93+
Lang Lang `json:"lang,select($any)"`
94+
}
95+
96+
type Lang struct {
97+
name [2]bool
98+
Code string `json:"code,select($any)"`
99+
}
100+
89101
func main() {
90102
//var testSlice = []int{}
91-
var testSlice = []Article{}
92-
j, _ := json.Marshal(filter.Select("t", testSlice))
93-
fmt.Println(string(j))
103+
//var testSlice = []Article{}
104+
//j, _ := json.Marshal(filter.Select("t", testSlice))
105+
//fmt.Println(string(j))
94106
//goOmit()
95107
//goSelect()
96108
//time.Sleep(time.Second * 10)
@@ -113,6 +125,16 @@ func main() {
113125
//for i := 0; i < 3; i++ {
114126
// ExampleOmit()
115127
//}
128+
129+
fmt.Println(filter.Omit("1", User{}))
130+
fmt.Println(filter.Omit("1", User{}))
131+
fmt.Println(filter.Omit("1", User{}))
132+
fmt.Println(filter.Omit("1", User{}))
133+
fmt.Println(filter.Omit("1", User{}))
134+
fmt.Println(filter.Omit("1", User{}))
135+
fmt.Println(filter.Omit("1", User{}))
136+
fmt.Println(filter.Omit("1", User{}))
137+
116138
}
117139

118140
func ExampleOmit() {

0 commit comments

Comments
 (0)