Skip to content

Commit

Permalink
远程函数:新增根据路径从当前对象取值的方法 getArgVal(String),方便 Long uid = getArgVal("User…
Browse files Browse the repository at this point in the history
…/id") 这样取值
  • Loading branch information
TommyLemon committed Jul 30, 2023
1 parent 47b36c9 commit c944c9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject
return this;
}

/**根据路径从当前对象取值
* @param path
* @return
* @param <T>
*/
public <T extends Object> T getArgVal(String path) {
return getArgVal(getCurrentObject(), path);
}
/**根据路径从对象 obj 中取值
* @param obj
* @param path
* @return
* @param <T>
*/
public static <T extends Object> T getArgVal(JSONObject obj, String path) {
return AbstractParser.getValue(obj, StringUtil.splitPath(path));
}


/**反射调用
* @param function 例如get(object,key),参数只允许引用,不能直接传值
Expand Down
6 changes: 3 additions & 3 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1683,10 +1683,10 @@ else if (join != null){
* @param pathKeys
* @return
*/
protected static Object getValue(JSONObject parent, String[] pathKeys) {
public static <V extends Object> V getValue(JSONObject parent, String[] pathKeys) {
if (parent == null || pathKeys == null || pathKeys.length <= 0) {
Log.w(TAG, "getChild parent == null || pathKeys == null || pathKeys.length <= 0 >> return parent;");
return parent;
return (V) parent;
}

//逐层到达child的直接容器JSONObject parent
Expand All @@ -1698,7 +1698,7 @@ protected static Object getValue(JSONObject parent, String[] pathKeys) {
parent = getJSONObject(parent, pathKeys[i]);
}

return parent == null ? null : parent.get(pathKeys[last]);
return parent == null ? null : (V) parent.get(pathKeys[last]);
}


Expand Down

0 comments on commit c944c9f

Please sign in to comment.