Skip to content

Commit 8e4d2db

Browse files
committed
Server:远程函数key():function(...)新增优先级key-() > key() > key+(),优化Function执行后的报错
1 parent 4173afd commit 8e4d2db

File tree

4 files changed

+120
-20
lines changed

4 files changed

+120
-20
lines changed

APIJSON-Java-Server/APIJSONDemo/src/main/java/apijson/demo/server/DemoFunction.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
package apijson.demo.server;
1616

17+
import java.util.Arrays;
1718
import java.util.List;
1819

1920
import com.alibaba.fastjson.JSONArray;
2021
import com.alibaba.fastjson.JSONObject;
2122

2223
import apijson.demo.server.model.BaseModel;
2324
import zuo.biao.apijson.Log;
25+
import zuo.biao.apijson.RequestRole;
2426
import zuo.biao.apijson.server.Function;
2527
import zuo.biao.apijson.server.NotNull;
2628

@@ -87,10 +89,33 @@ public static Object invoke(JSONObject request, String function) throws Exceptio
8789

8890

8991

90-
91-
public String search(@NotNull JSONObject request, String key) {
92+
93+
/**TODO 仅用来测试 "key-()":"getIdList()" 和 "key()":"getIdList()"
94+
* @param request
95+
* @return
96+
* @throws Exception
97+
*/
98+
public JSONArray getIdList(@NotNull JSONObject request) throws Exception {
99+
return new JSONArray(Arrays.asList(12, 15, 301, 82001, 82002, 38710)); //只能用JSONArray,用long[]会在SQLConfig解析崩溃
100+
}
101+
102+
/**TODO 仅用来测试 "key-()":"verifyAccess()"
103+
* @param request
104+
* @return
105+
* @throws Exception
106+
*/
107+
public Object verifyAccess(@NotNull JSONObject request) throws Exception {
108+
long userId = request.getLongValue(zuo.biao.apijson.JSONObject.KEY_USER_ID);
109+
RequestRole role = RequestRole.get(request.getString(zuo.biao.apijson.JSONObject.KEY_ROLE));
110+
if (userId != 70793 && role == RequestRole.ADMIN) {
111+
throw new IllegalAccessException("verifyAccess:ADMIN账号只能为70793!");
112+
}
92113
return null;
93114
}
115+
116+
117+
118+
94119

95120
public double plus(@NotNull JSONObject request, String i0, String i1) {
96121
return request.getDoubleValue(i0) + request.getDoubleValue(i1);
@@ -260,4 +285,7 @@ public double doubleValue(@NotNull JSONObject request, String value) {
260285
}
261286
//获取非基本类型对应基本类型的非空值 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
262287

288+
289+
290+
263291
}

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

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,21 @@ public boolean isBreakParse() {
193193
protected JSONObject response;
194194
protected JSONObject sqlRequest;
195195
protected JSONObject sqlReponse;
196+
/**
197+
* 自定义关键词
198+
*/
196199
protected Map<String, Object> customMap;
197-
protected Map<String, String> functionMap;
200+
/**
201+
* 远程函数
202+
* {"-":{ "key-()":value }, "0":{ "key()":value }, "+":{ "key+()":value } }
203+
* - : 在executeSQL前解析
204+
* 0 : 在executeSQL后、onChildParse前解析
205+
* + : 在onChildParse后解析
206+
*/
207+
protected Map<String, Map<String, String>> functionMap;
208+
/**
209+
* 子对象
210+
*/
198211
protected Map<String, JSONObject> childMap;
199212

200213
/**解析成员
@@ -221,7 +234,7 @@ public AbstractObjectParser parse() throws Exception {
221234
customMap = new LinkedHashMap<String, Object>();
222235
childMap = new LinkedHashMap<String, JSONObject>();
223236
}
224-
functionMap = new LinkedHashMap<String, String>();//必须执行
237+
functionMap = new LinkedHashMap<String, Map<String, String>>();//必须执行
225238

226239

227240
//条件<<<<<<<<<<<<<<<<<<<
@@ -283,6 +296,8 @@ else if (method == PUT && value instanceof JSONArray
283296
invalidate();//忽略错误,还原request
284297
}
285298
}
299+
300+
onFunctionResponse("-");
286301
}
287302
}
288303

@@ -352,10 +367,35 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
352367
if (value instanceof String == false) {
353368
throw new IllegalArgumentException(path + "/" + key + ":function() 后面必须为函数String!");
354369
}
355-
functionMap.put(key, (String) value);
356-
} else if (isTable && key.startsWith("@") && JSONRequest.TABLE_KEY_LIST.contains(key) == false) {
370+
371+
String k = key.substring(0, key.length() - 2);
372+
373+
String type; //远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里用 "-","0","+" 更直观,转用 -1,0,1 对性能提升不大。
374+
if (k.endsWith("-")) {
375+
type = "-";
376+
k = k.substring(0, k.length() - 1);
377+
}
378+
else if (k.endsWith("+")) {
379+
type = "+";
380+
k = k.substring(0, k.length() - 1);
381+
}
382+
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<>();
390+
}
391+
map.put(k, (String) value);
392+
393+
functionMap.put(type, map);
394+
}
395+
else if (isTable && key.startsWith("@") && JSONRequest.TABLE_KEY_LIST.contains(key) == false) {
357396
customMap.put(key, value);
358-
} else {
397+
}
398+
else {
359399
sqlRequest.put(key, value);
360400
}
361401

@@ -554,18 +594,45 @@ public JSONObject response() throws Exception {
554594
}
555595

556596

597+
onFunctionResponse("0");
598+
599+
onChildResponse();
600+
601+
onFunctionResponse("+");
602+
603+
onComplete();
604+
605+
return response;
606+
}
607+
608+
609+
@Override
610+
public void onFunctionResponse(String type) throws Exception {
611+
Map<String, String> map = functionMap == null ? null : functionMap.get(type);
557612

558613
//解析函数function
559-
if (functionMap != null) {
560-
Set<Entry<String, String>> functionSet = functionMap == null ? null : functionMap.entrySet();
561-
if (functionSet != null && functionSet.isEmpty() == false) {
562-
for (Entry<String, String> entry : functionSet) {
563-
response.put(AbstractSQLConfig.getRealKey(method, entry.getKey(), false, false)
564-
, onFunctionParse(response, entry.getValue()));
614+
Set<Entry<String, String>> functionSet = map == null ? null : map.entrySet();
615+
if (functionSet != null && functionSet.isEmpty() == false) {
616+
JSONObject json = "-".equals(type) ? request : response;
617+
618+
String key;
619+
Object value;
620+
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);
565629
}
566630
}
567631
}
568-
632+
}
633+
634+
@Override
635+
public void onChildResponse() throws Exception {
569636
//把isTable时取出去child解析后重新添加回来
570637
Set<Entry<String, JSONObject>> set = childMap == null ? null : childMap.entrySet();
571638
if (set != null) {
@@ -577,12 +644,9 @@ public JSONObject response() throws Exception {
577644
}
578645
}
579646
}
580-
581-
onComplete();
582-
583-
return response;
584647
}
585648

649+
586650
@Override
587651
public Object onFunctionParse(JSONObject json, String function) throws Exception {
588652
return null;
@@ -712,7 +776,7 @@ public Map<String, Object> getCustomMap() {
712776
return customMap;
713777
}
714778
@Override
715-
public Map<String, String> getFunctionMap() {
779+
public Map<String, Map<String, String>> getFunctionMap() {
716780
return functionMap;
717781
}
718782
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public static Object invoke(@NotNull Function fun, @NotNull JSONObject request,
9595
+ "\n调用时不要有空格!");
9696
}
9797
if (e instanceof InvocationTargetException) {
98+
Throwable te = ((InvocationTargetException) e).getTargetException();
99+
if (StringUtil.isEmpty(te.getMessage(), true) == false) { //到处把函数声明throws Exception改成throws Throwable挺麻烦
100+
throw te instanceof Exception ? (Exception) te : new Exception(te.getMessage());
101+
}
98102
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数传参类型错误!"
99103
+ "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(method, keys) + " 的要求!");
100104
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public interface ObjectParser {
117117
*/
118118
JSONObject response() throws Exception;
119119

120+
void onFunctionResponse(String type) throws Exception;
121+
122+
void onChildResponse() throws Exception;
123+
120124

121125
SQLConfig newSQLConfig() throws Exception;
122126

@@ -147,7 +151,7 @@ public interface ObjectParser {
147151
JSONObject getSqlReponse();
148152

149153
Map<String, Object> getCustomMap();
150-
Map<String, String> getFunctionMap();
154+
Map<String, Map<String, String>> getFunctionMap();
151155
Map<String, JSONObject> getChildMap();
152156

153157

0 commit comments

Comments
 (0)