@@ -6,8 +6,45 @@ type Filter struct {
6
6
node * fieldNodeTree
7
7
}
8
8
9
+ // Select 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示
10
+ func Select (selectScene string , el interface {}) interface {} {
11
+ return jsonFilter (selectScene , el , true )
12
+ }
13
+
14
+ func jsonFilter (selectScene string , el interface {}, isSelect bool ) Filter {
15
+ tree := & fieldNodeTree {
16
+ Key : "" ,
17
+ ParentNode : nil ,
18
+ }
19
+ tree .parseAny ("" , selectScene , el , isSelect )
20
+ return Filter {
21
+ node : tree ,
22
+ }
23
+ }
24
+
25
+ // Omit 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示
26
+ func Omit (omitScene string , el interface {}) interface {} {
27
+ return jsonFilter (omitScene , el , false )
28
+ }
29
+
30
+ // EnableCache 决定是否启用缓存,默认开启(强烈建议,除非万一缓存模式下出现bug,可以关闭缓存退回曾经的无缓存过滤模式),开启缓存后会有30%-40%的性能提升,开启缓存并没有副作用,只是会让结构体的字段tag常驻内存减少tag字符串处理操作
31
+ func EnableCache (enable bool ) {
32
+ enableCache = enable
33
+ }
34
+
35
+ // Deprecated
36
+ // SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
37
+ func SelectMarshal (selectScene string , el interface {}) Filter {
38
+ return jsonFilter (selectScene , el , true )
39
+ }
40
+
41
+ // Deprecated
42
+ // OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
43
+ func OmitMarshal (omitScene string , el interface {}) Filter {
44
+ return jsonFilter (omitScene , el , false )
45
+ }
9
46
func (f Filter ) MarshalJSON () ([]byte , error ) {
10
- return f .node .Bytes ( )
47
+ return useJSONMarshalFunc ( f .node .Marshal () )
11
48
}
12
49
13
50
// Deprecated
@@ -41,87 +78,3 @@ func (f Filter) String() string {
41
78
}
42
79
return json
43
80
}
44
-
45
- // Deprecated
46
- // SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
47
- func SelectMarshal (selectScene string , el interface {}) Filter {
48
- if enableCache {
49
- return selectWithCache (selectScene , el )
50
- }
51
- return selectMarshal (selectScene , el )
52
- }
53
- func selectMarshal (selectScene string , el interface {}) Filter {
54
- tree := & fieldNodeTree {
55
- Key : "" ,
56
- ParentNode : nil ,
57
- }
58
- tree .ParseSelectValue ("" , selectScene , el )
59
- return Filter {
60
- node : tree ,
61
- }
62
- }
63
-
64
- // Select 直接返回过滤后的数据结构,相当于直接SelectMarshal后再调用Interface方法
65
- func Select (selectScene string , el interface {}) interface {} {
66
- if enableCache {
67
- return selectWithCache (selectScene , el )
68
- }
69
- return selectMarshal (selectScene , el )
70
- }
71
-
72
- // selectWithCache 直接返回过滤后的数据结构,相当于直接SelectMarshal后再调用Interface方法
73
- func selectWithCache (selectScene string , el interface {}) Filter {
74
- tree := & fieldNodeTree {
75
- Key : "" ,
76
- ParentNode : nil ,
77
- }
78
- tree .ParseSelectValueWithCache ("" , selectScene , el )
79
- return Filter {
80
- node : tree ,
81
- }
82
- }
83
-
84
- // Omit 直接返回过滤后的数据结构,相当于直接OmitMarshal后再调用Interface方法
85
- func Omit (omitScene string , el interface {}) interface {} {
86
- if enableCache {
87
- return omitWithCache (omitScene , el )
88
- }
89
- return omitMarshal (omitScene , el )
90
- }
91
-
92
- // Deprecated
93
- // OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
94
- func OmitMarshal (omitScene string , el interface {}) Filter {
95
- if enableCache {
96
- return omitWithCache (omitScene , el )
97
- }
98
- return omitMarshal (omitScene , el )
99
- }
100
-
101
- func omitMarshal (omitScene string , el interface {}) Filter {
102
- tree := & fieldNodeTree {
103
- Key : "" ,
104
- ParentNode : nil ,
105
- }
106
- tree .ParseOmitValue ("" , omitScene , el )
107
- return Filter {
108
- node : tree ,
109
- }
110
- }
111
-
112
- // omitWithCache 第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
113
- func omitWithCache (omitScene string , el interface {}) Filter {
114
- tree := & fieldNodeTree {
115
- Key : "" ,
116
- ParentNode : nil ,
117
- }
118
- tree .ParseOmitValueWithCache ("" , omitScene , el )
119
- return Filter {
120
- node : tree ,
121
- }
122
- }
123
-
124
- // EnableCache 决定是否启用缓存,默认开启(强烈建议,除非万一缓存模式下出现bug,可以关闭缓存退回曾经的无缓存过滤模式),开启缓存后会有30%-40%的性能提升,开启缓存并没有副作用,只是会让结构体的字段tag常驻内存减少tag字符串处理操作
125
- func EnableCache (enable bool ) {
126
- enableCache = enable
127
- }
0 commit comments