Skip to content

Commit 5ce82da

Browse files
committed
Server:优化功能符解析顺序
1 parent 0762c97 commit 5ce82da

1 file changed

Lines changed: 57 additions & 44 deletions

File tree

  • APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/Parser.java

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,12 @@ && isInRelationMap(path) == false) {
509509

510510
boolean containRelation = false;
511511

512-
JSONObject transferredRequest = null;
512+
JSONObject transferredRequest = new JSONObject(true);//must init
513513
Map<String, String> functionMap = null;
514514
Map<String, Object> selfDefineKeyMap = null;
515515

516516
Set<String> set = new LinkedHashSet<>(request.keySet());
517517
if (set != null && set.size() > 0) {//判断换取少几个变量的初始化是否值得?
518-
transferredRequest = new JSONObject(true);
519518
functionMap = new LinkedHashMap<String, String>();
520519
selfDefineKeyMap = new LinkedHashMap<String, Object>();
521520

@@ -524,6 +523,10 @@ && isInRelationMap(path) == false) {
524523
boolean isFirst = true;
525524
for (String key : set) {
526525
value = request.get(key);
526+
if (value == null) {
527+
continue;
528+
}
529+
527530
if (value instanceof JSONObject) {//JSONObject,往下一级提取
528531
if (isArrayKey(key)) {//APIJSON Array
529532
result = getArray(path, config, key, (JSONObject) value);
@@ -592,63 +595,65 @@ && isInRelationMap(path) == false) {
592595
//PUT >>>>>>>>>>>>>>>>>>>>>>>>>
593596
}
594597
} else {//JSONArray或其它Object,直接填充
595-
transferredRequest.put(key, value);
596-
if (key.startsWith("@") && QueryConfig.TABLE_KEY_LIST.contains(key) == false) {
597-
selfDefineKeyMap.put(key, value);
598-
}//可@key@
599-
if (key.endsWith("()")) {
600-
if (value instanceof String == false) {
601-
throw new IllegalArgumentException("\"key()\": 后面必须为函数String!");
602-
}
603-
functionMap.put(key, (String) value);
604-
} else if (key.endsWith("@")) {//StringUtil.isPath((String) value)) {
598+
if (key.endsWith("@")) {//StringUtil.isPath((String) value)) {
605599
if (value instanceof String == false) {
606600
throw new IllegalArgumentException("\"key@\": 后面必须为依赖路径String!");
607601
}
608602
System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
609603
String replaceKey = key.substring(0, key.length() - 1);//key{}@ getRealKey(requestMethod, key, false, false);
604+
String keyPath = getAbsPath(path, replaceKey);
605+
String valuePath = new String((String) value);
606+
610607
if (parseRelation) {
611-
transferredRequest.remove(key);
612-
selfDefineKeyMap.remove(key);
613-
614-
Object target = getValueByPath(getRelationPath(getAbsPath(path, replaceKey)), true);
615-
Log.d(TAG, "getObject value = " + value + "; target = " + target);
616-
if (((String) value).equals(target) && isTableKey(table)) {
608+
Object target = getValueByPath(getRelationPath(keyPath), true);
609+
Log.d(TAG, "getObject valuePath = " + valuePath + "; target = " + target);
610+
if (valuePath.equals(target) && isTableKey(table)) {
617611
Log.e(TAG, "getObject ((String) value).equals(target) && isTableKey(table) >> return null;");
618612
return null;//parseRelation时还获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
619613
}
620-
if (key.startsWith("@")) {
621-
selfDefineKeyMap.put(replaceKey, target);
622-
} else {
623-
transferredRequest.put(replaceKey, target);
624-
}
614+
//还要isInRelationMap(path)判断 removeRelation(keyPath);
615+
updateRelation(getAbsPath(path, key), keyPath);//request结构已改变,需要更新依赖关系
625616

626-
//还要isInRelationMap(path)判断 removeRelation(getPath(path, replaceKey));
627-
updateRelation(path, getAbsPath(path, replaceKey));//request结构已改变,需要更新依赖关系
617+
key = replaceKey;
618+
value = target;
628619
} else {
620+
//尽量保留缺省依赖路径,这样就不需要担心路径改变
629621
//先尝试获取
630-
if (((String) value).startsWith(SEPARATOR)) {
631-
value = getAbsPath(parentPath, (String) value);
622+
if (valuePath.startsWith(SEPARATOR)) {
623+
valuePath = getAbsPath(parentPath, valuePath);
632624
}
633625
// Object target = getValueByPath((String) value);
634626
// Log.d(TAG, "getObject value = " + value + "; target = " + target);
635627
// if (target == null || ((String) value).equals(target)) {//标记并存放依赖关系
636628
containRelation = true;
637-
putRelation(getAbsPath(path, replaceKey), (String) value);
629+
putRelation(keyPath, valuePath);
638630
// } else {//直接替换原来的key@:path为key:target
639631
// transferredRequest.remove(key);
640632
// transferredRequest.put(replaceKey, target);
641633
// }
642634
}
643635
}
636+
637+
if (key.endsWith("()")) {
638+
if (value instanceof String == false) {
639+
throw new IllegalArgumentException("\"key()\": 后面必须为函数String!");
640+
}
641+
functionMap.put(key, (String) value);
642+
} else if (key.startsWith("@") && QueryConfig.TABLE_KEY_LIST.contains(key) == false) {
643+
selfDefineKeyMap.put(key, value);
644+
} else {
645+
transferredRequest.put(key, value);
646+
}
644647
}
645648
}
646649
}
647650

648651

652+
boolean query = false;
649653
//执行SQL操作数据库
650654
if (containRelation == false && isTableKey(table)) {//提高性能
651655
if (parseRelation == false || isInRelationMap(path)) {//避免覆盖原来已经获取的
656+
query = true;
652657
// keyValuePathMap.remove(path);
653658
QueryConfig config2 = newQueryConfig(table, transferredRequest);
654659

@@ -658,27 +663,35 @@ && isInRelationMap(path) == false) {
658663
}
659664

660665
transferredRequest = getSQLObject(config2);
666+
667+
if (transferredRequest == null) {
668+
transferredRequest = new JSONObject(true);
669+
}
670+
}
671+
}
661672

662-
if (transferredRequest != null && transferredRequest.isEmpty() == false) {//避免返回空的
663-
if (selfDefineKeyMap != null) {
664-
transferredRequest.putAll(selfDefineKeyMap);
665-
}
666-
//解析函数function
667-
Set<String> functionSet = functionMap == null ? null : functionMap.keySet();
668-
if (functionSet != null && functionSet.isEmpty() == false) {
669-
for (String key : functionSet) {
670-
transferredRequest.put(getRealKey(requestMethod, key, false, false)
671-
, Function.invoke(transferredRequest, functionMap.get(key)));
672-
}
673+
if (selfDefineKeyMap != null) {
674+
transferredRequest.putAll(selfDefineKeyMap);
675+
}
676+
if (functionMap != null) {
677+
if (query) {
678+
//解析函数function
679+
Set<String> functionSet = functionMap == null ? null : functionMap.keySet();
680+
if (functionSet != null && functionSet.isEmpty() == false) {
681+
for (String key : functionSet) {
682+
transferredRequest.put(getRealKey(requestMethod, key, false, false)
683+
, Function.invoke(transferredRequest, functionMap.get(key)));
673684
}
674-
675-
//可能客户端还有用,客户端自己解决,况且已知Android用fastjson解析不会崩溃 //移除自定义key
676-
putQueryResult(path, transferredRequest);//解决获取关联数据时requestObject里不存在需要的关联数据
677685
}
678-
686+
} else {
687+
transferredRequest.putAll(functionMap);
679688
}
680689
}
681690

691+
if (query) {
692+
putQueryResult(path, transferredRequest);//解决获取关联数据时requestObject里不存在需要的关联数据
693+
}
694+
682695
return transferredRequest;
683696
}
684697

@@ -871,7 +884,7 @@ public int estimateMaxCount(String path, String table, JSONObject value) throws
871884
}
872885
}
873886
}
874-
887+
875888
JSONObject response = new Parser(RequestMethod.HEAD).parseResponse(new JSONRequest(table, request));
876889
if (response != null) {
877890
response = response.getJSONObject(table);

0 commit comments

Comments
 (0)