forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructure.java
More file actions
executable file
·241 lines (210 loc) · 7.66 KB
/
Structure.java
File metadata and controls
executable file
·241 lines (210 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm;
import java.util.Map;
import java.util.regex.Pattern;
import javax.activation.UnsupportedDataTypeException;
import com.alibaba.fastjson.JSONObject;
import apijson.JSON;
import apijson.Log;
import apijson.NotNull;
import apijson.RequestMethod;
import apijson.orm.AbstractSQLConfig.Callback;
import apijson.orm.AbstractSQLConfig.IdCallback;
/**结构类。已整合进 AbstractVerifier,最快 4.5.0 移除
* 增删改查: OPERATION(ADD,REPLACE,PUT,REMOVE) OPERATION:{key0:value0, key1:value1 ...}
* 对值校验: VERIFY:{key0:value0, key1:value1 ...} (key{}:range,key$:"%m%"等)
* 对值重复性校验: UNIQUE:"key0:, key1 ..." (UNIQUE:"phone,email" 等)
* @author Lemon
*/
@Deprecated
public class Structure {
public static final String TAG = "Structure";
public static final Map<String, Pattern> COMPILE_MAP = AbstractVerifier.COMPILE_MAP;
private Structure() {}
/**从request提取target指定的内容
* @param method
* @param name
* @param target
* @param request
* @param creator
* @return
* @throws Exception
*/
public static JSONObject parseRequest(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject request, final SQLCreator creator) throws Exception {
return parseRequest(method, name, target, request, Parser.MAX_UPDATE_COUNT, creator);
}
/**从request提取target指定的内容
* @param method
* @param name
* @param target
* @param request
* @param maxUpdateCount
* @param creator
* @return
* @throws Exception
*/
public static JSONObject parseRequest(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject request, final int maxUpdateCount, final SQLCreator creator) throws Exception {
return parseRequest(method, name, target, request, maxUpdateCount, null, null, null, creator);
}
/**从request提取target指定的内容
* @param method
* @param name
* @param target
* @param request
* @param maxUpdateCount
* @param idKey
* @param userIdKey
* @param creator
* @return
* @throws Exception
*/
public static JSONObject parseRequest(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject request, final int maxUpdateCount
, final String database, final String schema, final IdCallback idCallback, final SQLCreator creator) throws Exception {
return AbstractVerifier.verifyRequest(method, name, target, request, maxUpdateCount, database, schema, idCallback, creator);
}
/**校验并将response转换为指定的内容和结构
* @param method
* @param name
* @param target
* @param response
* @param callback
* @param creator
* @return
* @throws Exception
*/
public static JSONObject parseResponse(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject response, SQLCreator creator, OnParseCallback callback) throws Exception {
return parseResponse(method, name, target, response, null, null, null, creator, callback);
}
/**校验并将response转换为指定的内容和结构
* @param method
* @param name
* @param target
* @param response
* @param idKey
* @param callback
* @param creator
* @return
* @throws Exception
*/
public static JSONObject parseResponse(@NotNull final RequestMethod method, final String name
, final JSONObject target, final JSONObject response, final String database, final String schema
, final Callback idKeyCallback, SQLCreator creator, OnParseCallback callback) throws Exception {
Log.i(TAG, "parseResponse method = " + method + "; name = " + name
+ "; target = \n" + JSON.toJSONString(target)
+ "\n response = \n" + JSON.toJSONString(response));
if (target == null || response == null) {// || target.isEmpty() {
Log.i(TAG, "parseRequest target == null || response == null >> return response;");
return response;
}
//解析
return parse(method, name, target, response, database, schema, idKeyCallback, creator, callback != null ? callback : new OnParseCallback() {});
}
/**对request和response不同的解析用callback返回
* @param method
* @param name
* @param target
* @param real
* @param creator
* @param callback
* @return
* @throws Exception
*/
public static JSONObject parse(@NotNull final RequestMethod method, String name, JSONObject target, JSONObject real
, SQLCreator creator, @NotNull OnParseCallback callback) throws Exception {
return parse(method, name, target, real, null, null, null, creator, callback);
}
/**对request和response不同的解析用callback返回
* @param method
* @param name
* @param target
* @param real
* @param idKey
* @param userIdKey
* @param creator
* @param callback
* @return
* @throws Exception
*/
public static JSONObject parse(@NotNull final RequestMethod method, String name, JSONObject target, JSONObject real
, final String database, final String schema, final IdCallback idCallback, SQLCreator creator, @NotNull OnParseCallback callback) throws Exception {
return AbstractVerifier.parse(method, name, target, real, database, schema, idCallback, creator, callback);
}
/**验证值类型
* @param tk
* @param tv {@link Operation}
* @param real
* @throws Exception
*/
public static void type(@NotNull String tk, Object tv, @NotNull JSONObject real) throws UnsupportedDataTypeException {
if (tv instanceof String == false) {
throw new UnsupportedDataTypeException("服务器内部错误," + tk + ":value 的value不合法!"
+ "Request表校验规则中 TYPE:{ key:value } 中的value只能是String类型!");
}
type(tk, (String) tv, real.get(tk));
}
/**验证值类型
* @param tk
* @param tv {@link Operation}
* @param rv
* @throws Exception
*/
public static void type(@NotNull String tk, @NotNull String tv, Object rv) throws UnsupportedDataTypeException {
type(tk, tv, rv, false);
}
/**验证值类型
* @param tk
* @param tv {@link Operation}
* @param rv
* @param isInArray
* @throws Exception
*/
public static void type(@NotNull String tk, @NotNull String tv, Object rv, boolean isInArray) throws UnsupportedDataTypeException {
AbstractVerifier.verifyType(tk, tv, rv, isInArray);
}
/**验证是否存在
* @param table
* @param key
* @param value
* @throws Exception
*/
public static void verifyExist(String table, String key, Object value, long exceptId, @NotNull SQLCreator creator) throws Exception {
AbstractVerifier.verifyExist(table, key, value, exceptId, creator);
}
/**验证是否重复
* @param table
* @param key
* @param value
* @throws Exception
*/
public static void verifyRepeat(String table, String key, Object value, @NotNull SQLCreator creator) throws Exception {
verifyRepeat(table, key, value, 0, creator);
}
/**验证是否重复
* @param table
* @param key
* @param value
* @param exceptId 不包含id
* @throws Exception
*/
public static void verifyRepeat(String table, String key, Object value, long exceptId, @NotNull SQLCreator creator) throws Exception {
verifyRepeat(table, key, value, exceptId, null, creator);
}
/**验证是否重复
* TODO 与 AbstractVerifier.verifyRepeat 代码重复,需要简化
* @param table
* @param key
* @param value
* @param exceptId 不包含id
* @param idKey
* @param creator
* @throws Exception
*/
public static void verifyRepeat(String table, String key, Object value, long exceptId, String idKey, @NotNull SQLCreator creator) throws Exception {
AbstractVerifier.verifyRepeat(table, key, value, exceptId, idKey, creator);
}
}