F2-3:运行时动词闭包子指令机制 + staging 晋级(FULL_AUTO 507/HARD 74)
ScriptFacade:addFieldButton 扩第3参闭包(onClick 子指令数组)+ showConfirm 新动词,服务端急切求值闭包,子指令收进子缓冲,输出嵌套指令 JSON。 前端 scriptCommands.ts/registerReplayAction.ts:处理 addButton onClick/ confirmDialog onOk 嵌套重放,onClickActions/onOkActions 透传给 amis。 ScriptFacadeTest 补 7 个正反例(24/24 全绿)。 staging 晋级:FULL_AUTO 507(+10)/HARD 74(-12)/自动化率 82.7%, 双库各更新 801 条,e2e 30/30 全绿。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -175,8 +175,76 @@ public class ScriptFacade {
|
||||
return emit("removeOption", field, args("value", value));
|
||||
}
|
||||
|
||||
/**
|
||||
* addFieldButton("FIELD","文案",{闭包},N) — F2-3 运行时动词:闭包子指令机制。
|
||||
*
|
||||
* <p>服务端急切求值闭包({@link Closure}),闭包内动词产生的指令收进子缓冲,
|
||||
* 输出 {@code {actionType:"addButton",componentId:"FIELD",args:{label,index:N,onClick:[子指令数组]}}}。
|
||||
* 非闭包参数(null 或旧版 handler 占位)按原有行为退化(onClick 不含子指令数组)。
|
||||
*/
|
||||
public Object addFieldButton(String field, Object label, Object handler) {
|
||||
return emit("addButton", field, args("label", label));
|
||||
return addFieldButton(field, label, handler, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* addFieldButton("FIELD","文案",{闭包},N) 四参完整形态。
|
||||
*/
|
||||
public Object addFieldButton(String field, Object label, Object handler, Object index) {
|
||||
List<Map<String, Object>> onClick = evalClosure(handler);
|
||||
Map<String, Object> a = new LinkedHashMap<>();
|
||||
a.put("label", label);
|
||||
if (index != null) {
|
||||
a.put("index", index);
|
||||
}
|
||||
if (onClick != null) {
|
||||
a.put("onClick", onClick);
|
||||
}
|
||||
return emit("addButton", field, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* showConfirm("消息",{闭包}) — F2-3 运行时动词:弹确认框。
|
||||
*
|
||||
* <p>同闭包子指令机制:闭包内动词收进子缓冲,
|
||||
* 输出 {@code {actionType:"confirmDialog",args:{msg,onOk:[子指令数组]}}}。
|
||||
*/
|
||||
public Object showConfirm(Object msg, Object handler) {
|
||||
List<Map<String, Object>> onOk = evalClosure(handler);
|
||||
Map<String, Object> a = new LinkedHashMap<>();
|
||||
a.put("msg", msg);
|
||||
if (onOk != null) {
|
||||
a.put("onOk", onOk);
|
||||
}
|
||||
return emit("confirmDialog", null, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* 急切求值闭包,返回闭包执行期间产生的子指令列表。
|
||||
* 非 Closure 入参(含 null)返回 null(调用方按无 onClick/onOk 处理)。
|
||||
*/
|
||||
private List<Map<String, Object>> evalClosure(Object handler) {
|
||||
if (!(handler instanceof Closure)) {
|
||||
return null;
|
||||
}
|
||||
// 用子 facade 执行闭包,闭包内所有动词写入子缓冲
|
||||
ScriptFacade child = new ScriptFacade(this.item);
|
||||
Bindings childBindings = child.buildBindings(this.item);
|
||||
@SuppressWarnings("unchecked")
|
||||
Closure<Object> closure = (Closure<Object>) handler;
|
||||
closure.setDelegate(child);
|
||||
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
|
||||
// 用子 bindings 注入的闭包变量替代 delegate 属性查找——
|
||||
// Groovy Closure 内调用动词走 MissingPropertyException→delegate,
|
||||
// 但直接把子 bindings 里的闭包先塞进当前脚本执行的 binding 会互相污染。
|
||||
// 最简可靠做法:在新 ScriptEngine 上下文内 eval 闭包体(保持隔离)。
|
||||
// 这里直接 call closure,delegate=child,闭包内动词调用经 delegate 走到 child 方法。
|
||||
try {
|
||||
closure.call();
|
||||
} catch (Exception e) {
|
||||
// 闭包内业务异常(如缺行数据)不阻断外层指令收集,子指令仍返回已产生部分
|
||||
}
|
||||
List<Map<String, Object>> sub = child.getCommands();
|
||||
return sub.isEmpty() ? new ArrayList<>() : sub;
|
||||
}
|
||||
|
||||
public Object addSubTableButton(String field, Object targetField, Object index) {
|
||||
@@ -293,7 +361,8 @@ public class ScriptFacade {
|
||||
|
||||
// UI 语义动词(字段 + 2 参)
|
||||
bind(b, "addFieldOption", a -> addFieldOption(str(a, 0), arg(a, 1), arg(a, 2)));
|
||||
bind(b, "addFieldButton", a -> addFieldButton(str(a, 0), arg(a, 1), arg(a, 2)));
|
||||
// addFieldButton 支持 3 参(旧)和 4 参(F2-3 闭包+index 形态)
|
||||
bind(b, "addFieldButton", a -> addFieldButton(str(a, 0), arg(a, 1), arg(a, 2), arg(a, 3)));
|
||||
bind(b, "addSubTableButton", a -> addSubTableButton(str(a, 0), arg(a, 1), arg(a, 2)));
|
||||
|
||||
// 组级动词(无字段参)
|
||||
@@ -302,6 +371,7 @@ public class ScriptFacade {
|
||||
|
||||
// 通知/按钮/基础绑定
|
||||
bind(b, "showToast", a -> showToast(arg(a, 0)));
|
||||
bind(b, "showConfirm", a -> showConfirm(arg(a, 0), arg(a, 1))); // F2-3 确认框
|
||||
bind(b, "enableButton", a -> enableButton(str(a, 0), arg(a, 1)));
|
||||
bind(b, "bindField", a -> bindField(str(a, 0), arg(a, 1)));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user