Skip to content

Commit c944c9f

Browse files
committed
远程函数:新增根据路径从当前对象取值的方法 getArgVal(String),方便 Long uid = getArgVal("User/id") 这样取值
1 parent 47b36c9 commit c944c9f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject
156156
return this;
157157
}
158158

159+
/**根据路径从当前对象取值
160+
* @param path
161+
* @return
162+
* @param <T>
163+
*/
164+
public <T extends Object> T getArgVal(String path) {
165+
return getArgVal(getCurrentObject(), path);
166+
}
167+
/**根据路径从对象 obj 中取值
168+
* @param obj
169+
* @param path
170+
* @return
171+
* @param <T>
172+
*/
173+
public static <T extends Object> T getArgVal(JSONObject obj, String path) {
174+
return AbstractParser.getValue(obj, StringUtil.splitPath(path));
175+
}
176+
159177

160178
/**反射调用
161179
* @param function 例如get(object,key),参数只允许引用,不能直接传值

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,10 +1683,10 @@ else if (join != null){
16831683
* @param pathKeys
16841684
* @return
16851685
*/
1686-
protected static Object getValue(JSONObject parent, String[] pathKeys) {
1686+
public static <V extends Object> V getValue(JSONObject parent, String[] pathKeys) {
16871687
if (parent == null || pathKeys == null || pathKeys.length <= 0) {
16881688
Log.w(TAG, "getChild parent == null || pathKeys == null || pathKeys.length <= 0 >> return parent;");
1689-
return parent;
1689+
return (V) parent;
16901690
}
16911691

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

1701-
return parent == null ? null : parent.get(pathKeys[last]);
1701+
return parent == null ? null : (V) parent.get(pathKeys[last]);
17021702
}
17031703

17041704

0 commit comments

Comments
 (0)