Skip to content

Commit 71278df

Browse files
committed
远程函数:新增支持 JavaScript 脚本,方便动态配置业务逻辑以及作为 Serveless 服务
1 parent ac86165 commit 71278df

3 files changed

Lines changed: 88 additions & 13 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>apijson.framework</groupId>
77
<artifactId>apijson-framework</artifactId>
8-
<version>5.4.0</version>
8+
<version>5.5.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>APIJSONFramework</name>
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.github.Tencent</groupId>
3636
<artifactId>APIJSON</artifactId>
37-
<version>5.4.0</version>
37+
<version>5.5.0</version>
3838
<type>jar</type>
3939
</dependency>
4040

src/main/java/apijson/framework/APIJSONConstant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import apijson.orm.model.PgAttribute;
2929
import apijson.orm.model.PgClass;
3030
import apijson.orm.model.Request;
31+
import apijson.orm.model.Script;
3132
import apijson.orm.model.SysColumn;
3233
import apijson.orm.model.SysTable;
3334
import apijson.orm.model.Table;
@@ -61,6 +62,7 @@ public class APIJSONConstant {
6162
public static final String DOCUMENT_;
6263
public static final String EXTENDED_PROPERTY_;
6364
public static final String FUNCTION_;
65+
public static final String SCRIPT_;
6466
public static final String PG_ATTRIBUTE_;
6567
public static final String PG_CLASS_;
6668
public static final String REQUEST_;
@@ -79,6 +81,7 @@ public class APIJSONConstant {
7981
DOCUMENT_ = Document.class.getSimpleName();
8082
EXTENDED_PROPERTY_ = ExtendedProperty.class.getSimpleName();
8183
FUNCTION_ = Function.class.getSimpleName();
84+
SCRIPT_ = Script.class.getSimpleName();
8285
PG_ATTRIBUTE_ = PgAttribute.class.getSimpleName();
8386
PG_CLASS_ = PgClass.class.getSimpleName();
8487
REQUEST_ = Request.class.getSimpleName();

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static apijson.RequestMethod.POST;
2323
import static apijson.RequestMethod.PUT;
2424
import static apijson.framework.APIJSONConstant.FUNCTION_;
25+
import static apijson.framework.APIJSONConstant.SCRIPT_;
2526

2627
import java.io.IOException;
2728
import java.rmi.ServerException;
@@ -145,24 +146,94 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
145146

146147
boolean isAll = table == null || table.isEmpty();
147148

148-
JSONObject function = isAll ? new JSONRequest() : table;
149-
if (Log.DEBUG == false) {
150-
function.put(APIJSONConstant.KEY_DEBUG, 0);
151-
}
152-
153-
JSONRequest functionItem = new JSONRequest();
154-
functionItem.put(FUNCTION_, function);
155-
156-
JSONObject request = new JSONObject();
157-
request.putAll(functionItem.toArray(0, 0, FUNCTION_));
149+
//JSONObject function = isAll ? new JSONRequest() : table;
150+
//if (Log.DEBUG == false) {
151+
// function.put(APIJSONConstant.KEY_DEBUG, 0);
152+
//}
153+
//
154+
//JSONRequest functionItem = new JSONRequest();
155+
//functionItem.put(FUNCTION_, function);
156+
//
157+
//JSONObject script = isAll ? new JSONRequest() : table;
158+
////if (Log.DEBUG == false) {
159+
//// script.put(APIJSONConstant.KEY_DEBUG, 0);
160+
////}
161+
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
162+
// JSONRequest nameInAt = new JSONRequest();
163+
// nameInAt.put("from", "Function");
164+
// { // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
165+
// JSONRequest fun = new JSONRequest();
166+
// fun.setColumn("name");
167+
// nameInAt.put("Function", fun);
168+
// } // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
169+
//
170+
// script.put("simple", 0);
171+
// script.put("name{}@", nameInAt);
172+
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
173+
//
174+
//JSONRequest scriptItem = new JSONRequest();
175+
//scriptItem.put(SCRIPT_, script);
176+
//
177+
JSONObject request = new JSONObject();
178+
//request.putAll(functionItem.toArray(0, 0, FUNCTION_));
179+
//request.putAll(scriptItem.toArray(0, 0, SCRIPT_));
180+
181+
// TODO 用这个来优化
182+
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
183+
JSONRequest item = new JSONRequest();
184+
185+
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
186+
JSONObject function = isAll ? new JSONRequest() : table;
187+
if (Log.DEBUG == false) {
188+
function.put(APIJSONConstant.KEY_DEBUG, 0);
189+
}
190+
item.put("Function", function);
191+
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
192+
193+
{ // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194+
JSONRequest script = new JSONRequest();
195+
script.put("name@", "/Function/name");
196+
script.put("simple", 0);
197+
item.put("Script", script);
198+
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
199+
200+
request.putAll(item.toArray(0, 0));
201+
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
158202

159203

160204
JSONObject response = creator.createParser().setMethod(GET).setNeedVerify(true).parseResponse(request);
161205
if (JSONResponse.isSuccess(response) == false) {
162206
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
163207
}
164208

165-
JSONArray list = response.getJSONArray(FUNCTION_ + "[]");
209+
JSONArray scriptList = response.getJSONArray("[]"); // .getJSONArray(SCRIPT_ + "[]");
210+
if (scriptList != null && scriptList.isEmpty() == false) {
211+
if (isAll) {
212+
SCRIPT_MAP = new LinkedHashMap<>();
213+
}
214+
215+
for (int i = 0; i < scriptList.size(); i++) {
216+
JSONObject item = scriptList.getJSONObject(i);
217+
item = item == null ? null : item.getJSONObject(SCRIPT_);
218+
if (item == null) {
219+
continue;
220+
}
221+
222+
String n = item == null ? null : item.getString("name");
223+
if (StringUtil.isName(n) == false) {
224+
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
225+
}
226+
227+
String s = item == null ? null : item.getString("script");
228+
if (StringUtil.isEmpty(s, true)) {
229+
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
230+
}
231+
232+
SCRIPT_MAP.put(n, item);
233+
}
234+
}
235+
236+
JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
166237
int size = list == null ? 0 : list.size();
167238
if (isAll && size <= 0) {
168239
Log.w(TAG, "init isAll && size <= 0,,没有可用的远程函数");
@@ -177,6 +248,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
177248

178249
for (int i = 0; i < size; i++) {
179250
JSONObject item = list.getJSONObject(i);
251+
item = item == null ? null : item.getJSONObject(FUNCTION_);
180252
if (item == null) {
181253
continue;
182254
}

0 commit comments

Comments
 (0)