Skip to content

Commit 347174e

Browse files
authored
Merge pull request #7 from h3110w0r1d-y/main
增加func过滤器
2 parents 64ed530 + c073e39 commit 347174e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

filter/parser.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,21 @@ TakeValueOfPointerValue: //这里主要是考虑到有可能用的不是一级
210210
ParentNode: t,
211211
IsAnonymous: isAnonymous,
212212
}
213-
214213
value := valueOf.Field(i)
214+
if tag.Function != "" {
215+
function := valueOf.MethodByName(tag.Function)
216+
if !function.IsValid() {
217+
if valueOf.CanAddr() {
218+
function = valueOf.Addr().MethodByName(tag.Function)
219+
}
220+
}
221+
if function.IsValid() {
222+
values := function.Call([]reflect.Value{})
223+
if len(values) != 0 {
224+
value = values[0]
225+
}
226+
}
227+
}
215228
if value.Kind() == reflect.Ptr {
216229
TakeFieldValue:
217230
if value.Kind() == reflect.Ptr {

filter/tag.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type tag struct {
2222
IsAnonymous bool
2323
////为空忽略
2424
Omitempty bool
25+
//自定义处理函数
26+
Function string
2527
}
2628

2729
func newSelectTag(tagStr, selectScene, fieldName string) tag {
@@ -55,10 +57,12 @@ func newSelectTag(tagStr, selectScene, fieldName string) tag {
5557
//说明选中了tag里的场景,不应该被忽略
5658
tagEl.IsOmitField = false
5759
tagEl.IsSelect = true
58-
return tagEl
5960
}
6061
}
6162
}
63+
if strings.HasPrefix(s, "func(") {
64+
tagEl.Function = s[5 : len(s)-1]
65+
}
6266
}
6367
return tagEl
6468
}
@@ -103,6 +107,9 @@ func newOmitTag(tagStr, omitScene, fieldName string) tag {
103107
}
104108
}
105109
}
110+
if strings.HasPrefix(s, "func(") {
111+
tagEl.Function = s[5 : len(s)-1]
112+
}
106113
}
107114
return tagEl
108115
}

test/main.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ type Us struct {
3838
//Uu Uu `json:"uu,select(h),omit(h)"`
3939
//S []string `json:"s,select(h)"`
4040

41-
BB [3]byte `json:"bb,select(all)"`
41+
BB [3]byte `json:"bb,select(all)"`
42+
Avatar []byte `json:"avatar,select(all),func(GetAvatar)"`
43+
Avatar2 []byte `json:"avatar2,select(all),func(GetAvatar2)"`
44+
UID UID `json:"uid,select(all)"`
45+
UIDs UIDs `json:"uids,select(all)"`
46+
}
4247

43-
UID UID `json:"uid,select(all)"`
44-
UIDs UIDs `json:"uids,select(all)"`
48+
func (u Us) GetAvatar() string {
49+
return string(u.Avatar[:]) + ".jpg"
50+
}
51+
func (u *Us) GetAvatar2() string {
52+
return string(u.Avatar[:]) + ".jpg"
4553
}
4654

4755
func newUs() Us {
@@ -64,13 +72,16 @@ func main() {
6472
B: []byte(`{"a":"1"}`),
6573
UID: UID{1, 3, 4},
6674
UIDs: UIDs{1, 23, 55},
75+
Avatar: []byte("uuid"),
76+
Avatar2: []byte("uuid2"),
6777
}
6878

6979
//fmt.Println(mustJson(u))
7080
//fmt.Println(filter.Omit("h", u))
7181
fmt.Println(filter.Select("all", u))
72-
7382
fmt.Println(filter.Omit("all", u))
83+
fmt.Println(filter.Select("all", &u))
84+
fmt.Println(filter.Omit("all", &u))
7485
TestSlice()
7586
TestMap()
7687
TestU()

0 commit comments

Comments
 (0)