Skip to content

Commit a40d4ed

Browse files
authored
Merge pull request #9 from h3110w0r1d-y/main
🐛 修复对interface{}类型的支持
2 parents 359eb60 + 57d7ab4 commit a40d4ed

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

filter/example_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,24 @@ func ExampleOmit() {
7070
//{"hot":120,"tags":[{"name":"foo"},{"name":"bar"}]}
7171
//{"hot":120,"tags":[{"icon":"icon"},{"icon":"icon"}]}
7272
}
73+
74+
type InterfaceTest struct {
75+
Data interface{} `json:"data"`
76+
}
77+
78+
func ExampleFilter_Interface() {
79+
80+
fmt.Println(Omit("", InterfaceTest{Data: map[string]interface{}{
81+
"1": 1,
82+
}}))
83+
fmt.Println(Omit("", InterfaceTest{Data: map[string]interface{}{
84+
"1": 1,
85+
"2": map[string]interface{}{
86+
"3": 3,
87+
},
88+
}}))
89+
//Output:
90+
//{"data":{"1":1}}
91+
//{"data":{"1":1,"2":{"3":3}}}
92+
93+
}

filter/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ TakePointerValue: //取指针的值
1818
valueOf = valueOf.Elem()
1919
typeOf = typeOf.Elem()
2020
goto TakePointerValue
21+
case reflect.Interface:
22+
valueOf = reflect.ValueOf(valueOf.Interface())
23+
typeOf = valueOf.Type()
24+
goto TakePointerValue
2125
case reflect.Struct:
2226
parserStruct(typeOf, valueOf, t, scene, key, isSelect)
2327
case reflect.Bool,

test/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func (u *Us) GetAvatar2() string {
4040
}
4141

4242
func main() {
43+
fmt.Println(filter.Omit("1111", Us2{Data: map[string]interface{}{
44+
"1": 1,
45+
}}))
46+
4347
//var bb = []byte(`{"a":"1"}`)
4448
u := Us{
4549
BB: [3]byte{1, 2, 4},

0 commit comments

Comments
 (0)