Skip to content

Commit 705fd94

Browse files
committed
Server:解决远程函数 fun-():function(...) 在非 Table:{} 对象内会在 onChildParse 后解析,导致 key@:"fun" 始终得不到值
1 parent 8e4d2db commit 705fd94

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/AbstractObjectParser.java

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public AbstractObjectParser parse() throws Exception {
230230

231231
Set<Entry<String, Object>> set = new LinkedHashSet<Entry<String, Object>>(request.entrySet());
232232
if (set != null && set.isEmpty() == false) {//判断换取少几个变量的初始化是否值得?
233-
if (isTable) {//非Table下不必分离出去再添加进来
233+
if (isTable) {//非Table下必须保证原有顺序!否则 count,page 会丢, total@:"/[]/total" 会在[]:{}前执行!
234234
customMap = new LinkedHashMap<String, Object>();
235235
childMap = new LinkedHashMap<String, JSONObject>();
236236
}
@@ -275,7 +275,8 @@ public AbstractObjectParser parse() throws Exception {
275275
if (value instanceof JSONObject && key.startsWith("@") == false) {//JSONObject,往下一级提取
276276
if (childMap != null) {//添加到childMap,最后再解析
277277
childMap.put(key, (JSONObject)value);
278-
} else {//直接解析并替换原来的
278+
}
279+
else {//直接解析并替换原来的,[]:{} 内必须直接解析,否则会因为丢掉count等属性,并且total@:"/[]/total"必须在[]:{} 后!
279280
response.put(key, onChildParse(index, key, (JSONObject)value));
280281
index ++;
281282
}
@@ -296,8 +297,8 @@ else if (method == PUT && value instanceof JSONArray
296297
invalidate();//忽略错误,还原request
297298
}
298299
}
299-
300-
onFunctionResponse("-");
300+
301+
//非Table内的函数会被滞后在onChildParse后调用! onFunctionResponse("-");
301302
}
302303
}
303304

@@ -367,30 +368,34 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
367368
if (value instanceof String == false) {
368369
throw new IllegalArgumentException(path + "/" + key + ":function() 后面必须为函数String!");
369370
}
370-
371+
371372
String k = key.substring(0, key.length() - 2);
372-
373+
373374
String type; //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里用 "-","0","+" 更直观,转用 -1,0,1 对性能提升不大。
374-
if (k.endsWith("-")) {
375+
if (k.endsWith("-")) { //不能封装到functionMap后批量执行,否则会导致非Table内的 key-():function() 在onChildParse后执行!
375376
type = "-";
376377
k = k.substring(0, k.length() - 1);
377-
}
378-
else if (k.endsWith("+")) {
379-
type = "+";
380-
k = k.substring(0, k.length() - 1);
378+
379+
parseFunction(request, k, (String) value);
381380
}
382381
else {
383-
type = "0";
384-
}
385-
386-
//远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
387-
Map<String, String> map = functionMap.get(type);
388-
if (map == null) {
389-
map = new LinkedHashMap<>();
382+
if (k.endsWith("+")) {
383+
type = "+";
384+
k = k.substring(0, k.length() - 1);
385+
}
386+
else {
387+
type = "0";
388+
}
389+
390+
//远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
391+
Map<String, String> map = functionMap.get(type);
392+
if (map == null) {
393+
map = new LinkedHashMap<>();
394+
}
395+
map.put(k, (String) value);
396+
397+
functionMap.put(type, map);
390398
}
391-
map.put(k, (String) value);
392-
393-
functionMap.put(type, map);
394399
}
395400
else if (isTable && key.startsWith("@") && JSONRequest.TABLE_KEY_LIST.contains(key) == false) {
396401
customMap.put(key, value);
@@ -595,9 +600,9 @@ public JSONObject response() throws Exception {
595600

596601

597602
onFunctionResponse("0");
598-
603+
599604
onChildResponse();
600-
605+
601606
onFunctionResponse("+");
602607

603608
onComplete();
@@ -613,24 +618,27 @@ public void onFunctionResponse(String type) throws Exception {
613618
//解析函数function
614619
Set<Entry<String, String>> functionSet = map == null ? null : map.entrySet();
615620
if (functionSet != null && functionSet.isEmpty() == false) {
616-
JSONObject json = "-".equals(type) ? request : response;
617-
618-
String key;
619-
Object value;
621+
// JSONObject json = "-".equals(type) ? request : response; // key-():function 是实时执行,而不是在这里批量执行
622+
620623
for (Entry<String, String> entry : functionSet) {
621-
622-
value = onFunctionParse(json, entry.getValue());
623-
624-
if (value != null) {
625-
key = AbstractSQLConfig.getRealKey(method, entry.getKey(), false, false);
626-
627-
response.put(key, value);
628-
parser.putQueryResult(AbstractParser.getAbsPath(path, key), value);
629-
}
624+
625+
// parseFunction(json, entry.getKey(), entry.getValue());
626+
parseFunction(response, entry.getKey(), entry.getValue());
630627
}
631628
}
632629
}
633-
630+
631+
public void parseFunction(JSONObject json, String key, String value) throws Exception {
632+
Object result = onFunctionParse(json, value);
633+
634+
if (result != null) {
635+
String k = AbstractSQLConfig.getRealKey(method, key, false, false);
636+
637+
response.put(k, result);
638+
parser.putQueryResult(AbstractParser.getAbsPath(path, k), result);
639+
}
640+
}
641+
634642
@Override
635643
public void onChildResponse() throws Exception {
636644
//把isTable时取出去child解析后重新添加回来

0 commit comments

Comments
 (0)