Skip to content

Commit ac9dc85

Browse files
committed
抽取根据 tag 自动包装请求结构的方法为 wrapRequest
1 parent af9a1fc commit ac9dc85

1 file changed

Lines changed: 39 additions & 16 deletions

File tree

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.alibaba.fastjson.JSONObject;
3131

3232
import apijson.JSON;
33+
import apijson.JSONRequest;
3334
import apijson.JSONResponse;
3435
import apijson.Log;
3536
import apijson.NotNull;
@@ -513,31 +514,53 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
513514
+ "非开放请求必须是后端 Request 表中校验规则允许的操作!\n " + error + "\n如果需要则在 Request 表中新增配置!");
514515
}
515516

516-
JSONObject target = object;
517-
if (object.containsKey(tag) == false) { //tag 是 Table 名或 Table[]
517+
//获取指定的JSON结构 >>>>>>>>>>>>>>
518+
JSONObject target = wrapRequest(object, tag, false);
519+
520+
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
521+
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), creator);
522+
}
518523

519-
boolean isArrayKey = tag.endsWith(":[]"); // JSONRequest.isArrayKey(tag);
520-
String key = isArrayKey ? tag.substring(0, tag.length() - 3) : tag;
521524

522-
if (apijson.JSONObject.isTableKey(key)) {
523-
if (isArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
524-
target.put(key + "[]", new JSONArray());
525-
}
526-
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
527-
target = new JSONObject(true);
528-
target.put(tag, object);
525+
/**自动根据 tag 是否为 TableKey 及是否被包含在 object 内来决定是否包装一层,改为 { tag: object, "tag": tag }
526+
* @param object
527+
* @param tag
528+
* @return
529+
*/
530+
public static JSONObject wrapRequest(JSONObject object, String tag, boolean putTag) {
531+
if (object == null || object.containsKey(tag)) { //tag 是 Table 名或 Table[]
532+
if (putTag) {
533+
if (object == null) {
534+
object = new JSONObject(true);
529535
}
536+
object.put(JSONRequest.KEY_TAG, tag);
530537
}
538+
return object;
531539
}
532540

533-
//获取指定的JSON结构 >>>>>>>>>>>>>>
534-
541+
boolean isDiffArrayKey = tag.endsWith(":[]");
542+
boolean isArrayKey = isDiffArrayKey || JSONRequest.isArrayKey(tag);
543+
String key = isArrayKey ? tag.substring(0, tag.length() - (isDiffArrayKey ? 3 : 2)) : tag;
535544

536-
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
537-
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), creator);
545+
JSONObject target = object;
546+
if (apijson.JSONObject.isTableKey(key)) {
547+
if (isDiffArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
548+
target.put(key + "[]", new JSONArray());
549+
}
550+
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
551+
target = new JSONObject(true);
552+
target.put(tag, object);
553+
}
554+
}
555+
556+
if (putTag) {
557+
target.put(JSONRequest.KEY_TAG, tag);
558+
}
559+
560+
return target;
538561
}
539562

540-
563+
541564
/**新建带状态内容的JSONObject
542565
* @param code
543566
* @param msg

0 commit comments

Comments
 (0)