Skip to content

Commit

Permalink
远程函数:getArgVal 新增支持传 Class<T> 返回指定类型,解决用缺省相对路径从当前请求的全局对象及 Parser 全局缓存…
Browse files Browse the repository at this point in the history
…取不到参数值
  • Loading branch information
TommyLemon committed Aug 20, 2023
1 parent 36d4972 commit c8c3b92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.*;

import static apijson.orm.AbstractSQLConfig.PATTERN_SCHEMA;
import static apijson.orm.SQLConfig.TYPE_ITEM;

/**可远程调用的函数类
* @author Lemon
Expand Down Expand Up @@ -156,28 +157,39 @@ public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject
return this;
}

/**根据路径从当前对象取值
/**根据路径取值
* @param path
* @return
* @param <T>
*/
public <T extends Object> T getArgVal(String path) {
return getArgVal(path, true); // 误判概率很小 false);
return getArgVal(path, null); // 误判概率很小 false);
}
/**根据路径取值
* @param path
* @param clazz
* @return
* @param <T>
*/
public <T extends Object> T getArgVal(String path, Class<T> clazz) {
return getArgVal(path, clazz, true);
}
/**根据路径取值
* @param path
* @param clazz
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser 缓存值
* @return
* @param <T>
*/
public <T extends Object> T getArgVal(String path, boolean tryAll) {
T val = getArgVal(getCurrentObject(), path);
public <T extends Object> T getArgVal(String path, Class<T> clazz, boolean tryAll) {
T val = getArgVal(getCurrentObject(), path, clazz);
if (tryAll == false || val != null) {
return val;
}

Parser<?> p = getParser();
return p == null ? null : (T) p.getValueByPath(path);
String targetPath = AbstractParser.getValuePath(getParentPath(), path);
return p == null ? null : (T) p.getValueByPath(targetPath);
}
/**根据路径从对象 obj 中取值
* @param obj
Expand All @@ -186,7 +198,11 @@ public <T extends Object> T getArgVal(String path, boolean tryAll) {
* @param <T>
*/
public static <T extends Object> T getArgVal(JSONObject obj, String path) {
return AbstractParser.getValue(obj, StringUtil.splitPath(path));
return getArgVal(obj, path, null);
}
public static <T extends Object> T getArgVal(JSONObject obj, String path, Class<T> clazz) {
Object v = AbstractParser.getValue(obj, StringUtil.splitPath(path));
return clazz == null ? (T) v : TypeUtils.cast(v, clazz, ParserConfig.getGlobalInstance());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ else if (isPlus) {
}

if (isPlus == false && isTable == false) {
parseFunction(key, k, (String) value, parentPath, name, request, isMinus);
parseFunction(key, k, (String) value, this.type == TYPE_ITEM ? path : parentPath, name, request, isMinus);
}
else {
//远程函数比较少用,一般一个Table:{}内用到也就一两个,所以这里循环里new出来对性能影响不大。
Expand Down Expand Up @@ -958,7 +958,7 @@ public void onFunctionResponse(String type) throws Exception {
JSONObject json = isMinus ? sqlRequest : response; // key-():function 是实时执行,而不是在这里批量执行

for (Entry<String, String> entry : functionSet) {
parseFunction(entry.getKey(), entry.getKey(), entry.getValue(), parentPath, name, json, isMinus);
parseFunction(entry.getKey(), entry.getKey(), entry.getValue(), this.type == TYPE_ITEM ? path : parentPath, name, json, isMinus);
}
}
}
Expand Down

0 comments on commit c8c3b92

Please sign in to comment.