Skip to content

Commit 790f8c5

Browse files
committed
全能 CRUD: "@gets": { "User":"" // 空字符串转为与 key 同名值 User },"@put": { "Moment":{} // 自动加 "tag":"Moment" },@post:"Comment[]" 自动转为 "@post":{ "Comment[]":"Comment:[]" }
1 parent 2b0d612 commit 790f8c5

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,9 +2190,10 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
21902190
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! 重复对象名为: " + key);
21912191
}
21922192

2193+
boolean isPost = apijson.orm.JSONRequest.KEY_POST.equals(key);
21932194
// @post、@get 等 RequestMethod
21942195
try {
2195-
RequestMethod keyMethod = apijson.orm.JSONRequest.KEY_METHOD_ENUM_MAP.get(key);
2196+
RequestMethod keyMethod = isPost ? RequestMethod.POST : JSONRequest.KEY_METHOD_ENUM_MAP.get(key);
21962197
if (keyMethod != null) {
21972198
// 如果不匹配,异常不处理即可
21982199
removeTmpKeys.add(key);
@@ -2209,7 +2210,9 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22092210
if (obj.containsKey(tbl)) {
22102211
throw new ConflictException(key + ": value 中 " + tbl + " 已经存在,不能重复!");
22112212
}
2212-
obj.put(tbl, new JSONObject(true));
2213+
2214+
obj.put(tbl, isPost && JSONRequest.isTableArray(tbl)
2215+
? tbl.substring(0, tbl.length() - 2) + ":[]" : "");
22132216
}
22142217
}
22152218
}
@@ -2234,15 +2237,16 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22342237
JSONObject objAttrJson = objVal instanceof JSONObject ? obj.getJSONObject(objKey) : null;
22352238
if (objAttrJson == null) {
22362239
if (objVal instanceof String) {
2237-
objAttrMap.put(JSONRequest.KEY_TAG, objVal);
2240+
objAttrMap.put(JSONRequest.KEY_TAG, "".equals(objVal) ? objKey : objVal);
22382241
}
22392242
else {
22402243
throw new IllegalArgumentException(key + ": { " + objKey + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !");
22412244
}
22422245
}
22432246
else {
2244-
Set<Entry<String, Object>> objSet = objAttrJson == null ? new HashSet<>() : objAttrJson.entrySet();
2247+
Set<Entry<String, Object>> objSet = objAttrJson.entrySet();
22452248

2249+
boolean hasTag = false;
22462250
for (Entry<String, Object> entry : objSet) {
22472251
String objAttrKey = entry == null ? null : entry.getKey();
22482252
if (objAttrKey == null) {
@@ -2255,13 +2259,21 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22552259
case apijson.JSONObject.KEY_DATABASE:
22562260
case JSONRequest.KEY_VERSION:
22572261
case apijson.JSONObject.KEY_ROLE:
2262+
objAttrMap.put(objAttrKey, entry.getValue());
2263+
break;
22582264
case JSONRequest.KEY_TAG:
2265+
hasTag = true;
22592266
objAttrMap.put(objAttrKey, entry.getValue());
22602267
break;
22612268
default:
22622269
break;
22632270
}
22642271
}
2272+
2273+
if (hasTag == false) {
2274+
objAttrMap.put(JSONRequest.KEY_TAG, isPost && JSONRequest.isTableArray(objKey)
2275+
? objKey.substring(0, objKey.length() - 2) + ":[]" : objKey);
2276+
}
22652277
}
22662278
}
22672279
continue;
@@ -2376,7 +2388,7 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
23762388
}
23772389
} catch (Exception e) {
23782390
e.printStackTrace();
2379-
throw new Exception(e);
2391+
throw new Exception(e); // 包装一层只是为了打印日志?看起来没必要
23802392
}
23812393
}
23822394

0 commit comments

Comments
 (0)