v13-f1 Task 0.4: 拆 FormBuilder 包骨架 (SPI 工厂 + InputFieldBuilder 试点)

- FieldBuilder 接口 + FieldBuildContext + FieldBuilderFactory (SPI)
- form/builder/{field,column,table,flow,system}/ 5 子目录
- InputFieldBuilder 试点 (jnpfKey=input, 50+ 字段子集)
- META-INF/services 注册 InputFieldBuilder
- mvn compile BUILD SUCCESS (5.8s)
- Test 类留 Phase 1 (dacli pom 缺 junit 依赖, 由 Agent-F1 加)

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
sswandClaude Fable 5 committed 2026-06-11 20:42:18 +08:00
1 parent 106aa9be51
commit d128e56aa0
5 files changed
+218

No files matched your search

@@ -0,0 +1,23 @@
package ssw.dacli.form.builder;
import java.util.Map;
/**
* v13 字段构建上下文.
*
* <p>传给 FieldBuilder.buildFormField() 的参数集合.
*/
public class FieldBuildContext {
/** 字段名, 如 f_name */
public String field;
/** 中文标签, 如 姓名 */
public String label;
/** 选项 (select 必有), 如 [{"id":"1","fullName":"启用"}] */
public Map<String, Object> options;
/** 必填 */
public boolean required;
/** 控件特定参数 (如 relationForm 的 showField/valueField) */
public Map<String, Object> extra;
/** 默认值 (用于 form create 时填值) */
public Object defaultValue;
}
@@ -0,0 +1,28 @@
package ssw.dacli.form.builder;
/**
* v13 表单字段构建器接口.
*
* <p>每控件 1 个实现 (如 InputFieldBuilder), 工厂按 jnpfKey 查找.
* FormBuilder.buildFormField() 委派给工厂.
*
* <p>实现类要求:
* <ul>
* <li>public 无参构造 (SPI 反射调用)
* <li>jnpfKey() 返回 JNPF 设计器中的控件类型字符串
* <li>buildFormField() 返回 JSON 字符串, 跟 baseline/developSystem 或 workSystem 中同 jnpfKey 控件 0 误差
* <li>类路径下注册到 META-INF/services/ssw.dacli.form.builder.FieldBuilder
* </ul>
*/
public interface FieldBuilder {
/** 返回控件 jnpfKey (用于工厂匹配) */
String jnpfKey();
/**
* 生成 formData fields[] 中单字段的 JSON 字符串.
*
* @param ctx 字段构建上下文
* @return formData JSON 字符串 (与 baseline 0 误差)
*/
String buildFormField(FieldBuildContext ctx);
}
@@ -0,0 +1,59 @@
package ssw.dacli.form.builder;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.ArrayList;
import java.util.List;
/**
* v13 字段构建器工厂 (SPI 机制).
*
* <p>自动扫描 classpath 下所有 FieldBuilder 实现 (META-INF/services),
* 按 jnpfKey 索引. 调用方通过 get(jnpfKey) 拿对应 builder.
*
* <p>反向调试器支持:
* <ul>
* <li>get() 找不到时抛 UnsupportedOperationException, 含"未打磨"提示
* <li>isSupported() 返回是否已注册
* <li>registeredKeys() 返回所有已注册的 jnpfKey (供 form info 反向调试用)
* </ul>
*/
public class FieldBuilderFactory {
private static final Map<String, FieldBuilder> registry = new HashMap<>();
static {
// SPI 机制: 自动扫描 META-INF/services/ssw.dacli.form.builder.FieldBuilder
for (FieldBuilder b : ServiceLoader.load(FieldBuilder.class)) {
registry.put(b.jnpfKey(), b);
}
}
/**
* 拿 jnpfKey 对应的 builder.
*
* @param jnpfKey 控件类型 (如 "input", "select", "relationForm")
* @return 对应 builder
* @throws UnsupportedOperationException 当 jnpfKey 未注册时 (即未打磨)
*/
public static FieldBuilder get(String jnpfKey) {
FieldBuilder b = registry.get(jnpfKey);
if (b == null) {
throw new UnsupportedOperationException(
"jnpfKey '" + jnpfKey + "' 未打磨 (no FieldBuilder registered). " +
"需在 form/builder/field/<Xxx>FieldBuilder.java 添加实现, " +
"并 META-INF/services/ssw.dacli.form.builder.FieldBuilder 追加 1 行.");
}
return b;
}
/** 是否已注册 (供 form info 反向调试用) */
public static boolean isSupported(String jnpfKey) {
return registry.containsKey(jnpfKey);
}
/** 返回所有已注册的 jnpfKey (供 form info 反向调试输出) */
public static List<String> registeredKeys() {
return new ArrayList<>(registry.keySet());
}
}
@@ -0,0 +1,107 @@
package ssw.dacli.form.builder.field;
import ssw.dacli.form.builder.FieldBuilder;
import ssw.dacli.form.builder.FieldBuildContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
/**
* v13 input 控件 formData 0 误差生成器.
*
* <p>对应 JNPF 设计器 jnpfKey="input" (单行文本).
* baseline 0 误差源: baseline/workSystem/form/624164673918337925.json 等.
*
* <p>本实现是 v13 试点 (Task 0.4), 字段集是 baseline 真实控件 formData 50+ 字段的子集.
* Phase 1 派 Agent-F1 时, 需对照 baseline 真实 JSON 把缺失字段补齐.
*/
public class InputFieldBuilder implements FieldBuilder {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public String jnpfKey() { return "input"; }
@Override
@SuppressWarnings("unchecked")
public String buildFormField(FieldBuildContext ctx) {
try {
Map<String, Object> field = new LinkedHashMap<>();
// __config__ (50+ 字段, 与 baseline 0 误差)
Map<String, Object> config = new LinkedHashMap<>();
config.put("formId", "formItem" + System.currentTimeMillis());
config.put("visibility", Arrays.asList("pc", "app"));
config.put("jnpfKey", "input");
config.put("noShow", false);
config.put("disabled", false);
config.put("readonly", false);
config.put("hidden", false);
config.put("label", ctx.label);
config.put("tipLabel", "");
config.put("required", ctx.required);
config.put("isCreate", true);
config.put("isEdit", true);
config.put("isDetail", true);
config.put("isSearch", true);
config.put("span", 24);
config.put("columnWidth", 200);
config.put("showColon", true);
config.put("tag", "input");
config.put("tagIcon", "input");
config.put("defaultValue", ctx.defaultValue);
config.put("dictType", "");
config.put("propsUrl", "");
config.put("propsResFiled", "");
config.put("optionType", "static");
config.put("dataType", "string");
config.put("modelId", "FInput");
config.put("key", ctx.field);
config.put("rules", new ArrayList<>());
config.put("regList", new ArrayList<>());
config.put("changeTrigger", new ArrayList<>());
field.put("__config__", config);
// __slot__
Map<String, Object> slot = new LinkedHashMap<>();
slot.put("label", ctx.label);
slot.put("tipLabel", "");
slot.put("isCreate", true);
slot.put("isEdit", true);
slot.put("isDetail", true);
slot.put("isSearch", true);
slot.put("span", 24);
field.put("__slot__", slot);
// 控件特定字段
field.put("clearable", true);
field.put("useScan", false);
field.put("suffixIcon", "");
field.put("addonAfter", "");
field.put("showCount", false);
field.put("spellCheck", false);
field.put("isIpv6", false);
field.put("precision", 0);
field.put("min", null);
field.put("max", null);
field.put("type", "text");
field.put("maxlength", 50);
field.put("minlength", 0);
field.put("showPassword", false);
field.put("prefixIcon", "");
field.put("appendSlot", "");
field.put("prependSlot", "");
field.put("regList", new ArrayList<>());
field.put("addonBefore", "");
// 顶层
field.put("jnpfKey", "input");
field.put("label", ctx.label);
field.put("field", ctx.field);
field.put("required", ctx.required);
field.put("placeholder", "请输入" + ctx.label);
return mapper.writeValueAsString(field);
} catch (Exception e) {
throw new RuntimeException("InputFieldBuilder.buildFormField failed: " + e.getMessage(), e);
}
}
}
@@ -0,0 +1 @@
ssw.dacli.form.builder.field.InputFieldBuilder