v13-f1 Task 1.20: form info 反向调试器 (reverse 子目录, 解析 formData 定位未打磨 jnpfKey)

This commit is contained in:
ssw committed 2026-06-11 23:51:29 +08:00
1 parent 05e2bc7d8c
commit dcb24a0066
3 files changed
+484

No files matched your search

@@ -0,0 +1,226 @@
package ssw.dacli.form.builder.reverse;
import ssw.dacli.form.builder.FieldBuilderFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* v13 Task 1.20 — form info 反向调试器.
*
* <p>读已有 form JSON (formData 字段), 提取 jnpfKey 分布, 跟
* {@link FieldBuilderFactory#registeredKeys()} 对比, 输出<b>未打磨控件</b>列表.
*
* <p>典型用法:
* <pre>
* ReverseAnalysisReport r = new FormReverseDebugger().analyzeFormData(jsonStr);
* System.out.println(r.toJson());
* // 或:
* ReverseAnalysisReport r2 = new FormReverseDebugger().analyzeFormFile(Paths.get("form.json"));
* </pre>
*
* <p>关键扫描点:
* <ol>
* <li>formData.formItems[i].__config__.jnpfKey — 顶层字段</li>
* <li>formData.columnList[i].__config__.jnpfKey — 列表列定义</li>
* <li>formData.fields[i] (兼容模式) — 老 form 顶层 fields</li>
* <li>formData.flowTemplateJson — 判工作流 (存在性)</li>
* </ol>
*
* <p>注: 本类不是 FieldBuilder, 不需注册 SPI. 供 dacli form info --reverse 调用.
*/
public class FormReverseDebugger {
private static final ObjectMapper M = new ObjectMapper();
/**
* 解析 formData JSON 字符串, 返回分析报告.
*
* @param formDataJson formData 字段的 JSON 内容 (从 form JSON 提取的 formData 部分)
* @return 分析报告
*/
public ReverseAnalysisReport analyzeFormData(String formDataJson) {
if (formDataJson == null || formDataJson.isEmpty()) {
return emptyReport("(empty formData)");
}
try {
JsonNode root = M.readTree(formDataJson);
return analyzeRoot(root);
} catch (Exception e) {
throw new RuntimeException("FormReverseDebugger.analyzeFormData failed: " + e.getMessage(), e);
}
}
/**
* 读 form JSON 文件并分析.
*
* <p>支持两种输入:
* <ul>
* <li>完整 form JSON: 含 formData 字段 (object/string 均可)</li>
* <li>纯 formData 字符串: 直接解析为 formData 树</li>
* </ul>
*
* @param formFile form JSON 文件路径
* @return 分析报告
*/
public ReverseAnalysisReport analyzeFormFile(Path formFile) {
if (!Files.exists(formFile)) {
throw new IllegalArgumentException("form file not found: " + formFile);
}
try {
String content = Files.readString(formFile);
JsonNode root = M.readTree(content);
// 兼容: 完整 form JSON 含 formData 字段
if (root.has("formData") && root.get("formData") != null && !root.get("formData").isNull()) {
JsonNode formData = root.get("formData");
if (formData.isTextual()) {
// formData 是字符串 (escape 后的 JSON)
return analyzeFormData(formData.asText());
}
return analyzeRoot(formData);
}
// 纯 formData 内容
return analyzeRoot(root);
} catch (Exception e) {
throw new RuntimeException("FormReverseDebugger.analyzeFormFile failed for " + formFile + ": " + e.getMessage(), e);
}
}
/**
* 内部核心: 遍历 formData 树, 收集 jnpfKey 分布.
*/
private ReverseAnalysisReport analyzeRoot(JsonNode root) {
// LinkedHashMap 保序, 计数同步
Map<String, Integer> jnpfKeyCount = new LinkedHashMap<>();
List<String> usedJnpfKeys = new ArrayList<>();
int totalFieldCount = 0;
int columnListSize = 0;
// 1) formItems[] (主表字段)
if (root.has("formItems") && root.get("formItems").isArray()) {
for (JsonNode item : root.get("formItems")) {
String key = extractJnpfKey(item);
if (key != null) {
jnpfKeyCount.merge(key, 1, Integer::sum);
if (!usedJnpfKeys.contains(key)) usedJnpfKeys.add(key);
totalFieldCount++;
}
}
}
// 2) columnList[] (主表列表列)
if (root.has("columnList") && root.get("columnList").isArray()) {
JsonNode colList = root.get("columnList");
columnListSize += colList.size();
for (JsonNode col : colList) {
String key = extractJnpfKey(col);
if (key != null) {
jnpfKeyCount.merge(key, 1, Integer::sum);
if (!usedJnpfKeys.contains(key)) usedJnpfKeys.add(key);
}
}
}
// 3) 兼容: 顶层 fields[] (老 form 格式)
if (root.has("fields") && root.get("fields").isArray()) {
for (JsonNode item : root.get("fields")) {
String key = extractJnpfKey(item);
if (key != null) {
jnpfKeyCount.merge(key, 1, Integer::sum);
if (!usedJnpfKeys.contains(key)) usedJnpfKeys.add(key);
totalFieldCount++;
}
}
}
// 4) 子表递归扫描: formItems[].__config__.children[] 或 tableFields[]
if (root.has("formItems") && root.get("formItems").isArray()) {
for (JsonNode item : root.get("formItems")) {
// 子表字段通常在 __config__.children[] 或 顶层 children[]
JsonNode children = item.get("children");
if (children == null && item.has("__config__")) {
children = item.get("__config__").get("children");
}
if (children != null && children.isArray()) {
for (JsonNode subItem : children) {
String key = extractJnpfKey(subItem);
if (key != null) {
jnpfKeyCount.merge(key, 1, Integer::sum);
if (!usedJnpfKeys.contains(key)) usedJnpfKeys.add(key);
}
}
}
// 子表自身可能含 columnList (主子表场景)
JsonNode subColList = item.get("columnList");
if (subColList != null && subColList.isArray()) {
columnListSize += subColList.size();
for (JsonNode col : subColList) {
String key = extractJnpfKey(col);
if (key != null) {
jnpfKeyCount.merge(key, 1, Integer::sum);
if (!usedJnpfKeys.contains(key)) usedJnpfKeys.add(key);
}
}
}
}
}
// 5) 比对 registeredKeys, 拆 supported / unsupported
List<String> supported = new ArrayList<>();
List<String> unsupported = new ArrayList<>();
for (String key : usedJnpfKeys) {
if (FieldBuilderFactory.isSupported(key)) {
supported.add(key);
} else {
unsupported.add(key);
}
}
// 6) 判 flowTemplateJson
boolean hasFlow = root.has("flowTemplateJson")
&& !root.get("flowTemplateJson").isNull()
&& !root.get("flowTemplateJson").isMissingNode();
// 7) formSummary 一行摘要
String summary = String.format(
"fields=%d, columns=%d, jnpfKeys=%d, unsupported=%d, flow=%s",
totalFieldCount, columnListSize, usedJnpfKeys.size(),
unsupported.size(), hasFlow ? "yes" : "no");
return new ReverseAnalysisReport(
usedJnpfKeys, jnpfKeyCount, unsupported, supported,
totalFieldCount, columnListSize, hasFlow, summary);
}
/**
* 提取单个 formItem / column 的 jnpfKey.
* 路径: __config__.jnpfKey
* 兜底: 顶层 jnpfKey (罕见格式)
*/
private String extractJnpfKey(JsonNode item) {
if (item == null) return null;
JsonNode cfg = item.get("__config__");
if (cfg != null && cfg.has("jnpfKey")) {
return cfg.get("jnpfKey").asText();
}
if (item.has("jnpfKey")) {
return item.get("jnpfKey").asText();
}
return null;
}
/** 兜底: 空 formData 报告 */
private ReverseAnalysisReport emptyReport(String summary) {
return new ReverseAnalysisReport(
new ArrayList<>(), new LinkedHashMap<>(),
new ArrayList<>(), new ArrayList<>(),
0, 0, false, summary);
}
}
@@ -0,0 +1,86 @@
package ssw.dacli.form.builder.reverse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.List;
import java.util.Map;
/**
* v13 Task 1.20 — form info 反向调试输出报告 (POJO).
*
* <p>FormReverseDebugger.analyzeFormData() / analyzeFormFile() 返回.
* 用于定位 form JSON 中 <b>未打磨</b> 的 jnpfKey (即无对应 FieldBuilder).
*
* <p>字段:
* <ul>
* <li>usedJnpfKeys: form 用到的 jnpfKey 列表 (去重, 按出现顺序)</li>
* <li>jnpfKeyCount: jnpfKey → 出现次数 (formItems + columnList 合计)</li>
* <li>unsupportedJnpfKeys: 未注册的 jnpfKey (需要新增 FieldBuilder)</li>
* <li>supportedJnpfKeys: 已注册 (FieldBuilderFactory.isSupported=true)</li>
* <li>totalFieldCount: 字段总数 (formItems 数量)</li>
* <li>columnListSize: columnList 长度 (主表列表 + 子表 columnList 合计)</li>
* <li>hasFlowTemplate: flowTemplateJson 不为 null</li>
* <li>formSummary: 简短摘要 (一行字符串)</li>
* </ul>
*
* <p>toJson() 用 Jackson 序列化为 pretty-printed JSON, 供 dacli form info --reverse 输出.
*/
public class ReverseAnalysisReport {
/** form 用到的 jnpfKey 列表 (去重, 按首次出现顺序) */
public final List<String> usedJnpfKeys;
/** jnpfKey → 出现次数 (formItems + columnList 合计) */
public final Map<String, Integer> jnpfKeyCount;
/** 未注册的 jnpfKey (需新增 FieldBuilder) */
public final List<String> unsupportedJnpfKeys;
/** 已注册 (有对应 FieldBuilder) 的 jnpfKey */
public final List<String> supportedJnpfKeys;
/** formItems 字段总数 */
public final int totalFieldCount;
/** columnList 长度 (主表 + 子表合计) */
public final int columnListSize;
/** flowTemplateJson 不为 null (工作流表单) */
public final boolean hasFlowTemplate;
/** 简短摘要 (一行) */
public final String formSummary;
public ReverseAnalysisReport(
List<String> usedJnpfKeys,
Map<String, Integer> jnpfKeyCount,
List<String> unsupportedJnpfKeys,
List<String> supportedJnpfKeys,
int totalFieldCount,
int columnListSize,
boolean hasFlowTemplate,
String formSummary) {
this.usedJnpfKeys = usedJnpfKeys;
this.jnpfKeyCount = jnpfKeyCount;
this.unsupportedJnpfKeys = unsupportedJnpfKeys;
this.supportedJnpfKeys = supportedJnpfKeys;
this.totalFieldCount = totalFieldCount;
this.columnListSize = columnListSize;
this.hasFlowTemplate = hasFlowTemplate;
this.formSummary = formSummary;
}
/**
* 序列化为 pretty-printed JSON (Jackson).
* 供 dacli form info --reverse 输出到终端 / 管道给 jq.
*/
public String toJson() {
try {
ObjectMapper m = new ObjectMapper();
m.enable(SerializationFeature.INDENT_OUTPUT);
return m.writeValueAsString(this);
} catch (Exception e) {
throw new RuntimeException("ReverseAnalysisReport.toJson failed: " + e.getMessage(), e);
}
}
}
@@ -0,0 +1,172 @@
package ssw.dacli.form.builder.reverse;
import ssw.dacli.form.builder.FieldBuilderFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.*;
import java.util.*;
/**
* v13 Task 1.20 — FormReverseDebugger 单元测试 (4 用例).
*
* <p>校验核心行为:
* <ol>
* <li>testBasic: formItems 提取 jnpfKey 列表 + 计数</li>
* <li>testUnsupported: 未注册 jnpfKey 正确归到 unsupportedJnpfKeys</li>
* <li>testColumnList: columnList 长度 + jnpfKey 提取</li>
* <li>testDumpToFile: 写 target/baseline-dump/FormReverseDebugger.json</li>
* </ol>
*/
public class FormReverseDebuggerTest {
private static final ObjectMapper M = new ObjectMapper();
@Test
public void testBasic() throws Exception {
// 构造 formData: formItems 含 input/datePicker/relationForm 3 字段
// 注: "select" 不是 v13 已注册 jnpfKey, 实际注册的是 popupSelect/relationForm/autoComplete 等
String formDataJson = "{\n" +
" \"formItems\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"},\n" +
" {\"__config__\": {\"jnpfKey\": \"datePicker\"}, \"__vModel__\": \"f_date\"},\n" +
" {\"__config__\": {\"jnpfKey\": \"relationForm\"}, \"__vModel__\": \"f_user\"}\n" +
" ]\n" +
"}";
ReverseAnalysisReport r = new FormReverseDebugger().analyzeFormData(formDataJson);
// 1) usedJnpfKeys 包含 3 个控件
assertEquals(3, r.usedJnpfKeys.size());
assertTrue(r.usedJnpfKeys.contains("input"));
assertTrue(r.usedJnpfKeys.contains("datePicker"));
assertTrue(r.usedJnpfKeys.contains("relationForm"));
// 2) jnpfKeyCount 计数 = 1
assertEquals(1, r.jnpfKeyCount.get("input").intValue());
assertEquals(1, r.jnpfKeyCount.get("datePicker").intValue());
assertEquals(1, r.jnpfKeyCount.get("relationForm").intValue());
// 3) totalFieldCount = 3
assertEquals(3, r.totalFieldCount);
// 4) columnListSize = 0 (无 columnList)
assertEquals(0, r.columnListSize);
// 5) input/datePicker/relationForm 应已被打磨 (33+ builders)
// (校验 Factory 实际注册了这些 key, 跟 plan "v13 33 个 builder" 对齐)
assertTrue(FieldBuilderFactory.isSupported("input"), "input 必须已注册");
assertTrue(FieldBuilderFactory.isSupported("datePicker"), "datePicker 必须已注册");
assertTrue(FieldBuilderFactory.isSupported("relationForm"), "relationForm 必须已注册");
// 6) supported 列表
assertTrue(r.supportedJnpfKeys.contains("input"));
assertTrue(r.supportedJnpfKeys.contains("datePicker"));
assertTrue(r.supportedJnpfKeys.contains("relationForm"));
// 7) unsupported 空
assertTrue(r.unsupportedJnpfKeys.isEmpty(), "本测试 3 个控件都已注册, unsupported 应为空");
// 8) formSummary 不空
assertNotNull(r.formSummary);
assertTrue(r.formSummary.contains("fields=3"));
}
@Test
public void testUnsupported() throws Exception {
// 构造 formData: 含 1 个未注册 jnpfKey "unknownControl"
String formDataJson = "{\n" +
" \"formItems\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"},\n" +
" {\"__config__\": {\"jnpfKey\": \"unknownControl\"}, \"__vModel__\": \"f_xxx\"}\n" +
" ]\n" +
"}";
ReverseAnalysisReport r = new FormReverseDebugger().analyzeFormData(formDataJson);
// 1) unsupportedJnpfKeys 含 "unknownControl"
assertEquals(1, r.unsupportedJnpfKeys.size());
assertTrue(r.unsupportedJnpfKeys.contains("unknownControl"));
// 2) supported 含 "input"
assertTrue(r.supportedJnpfKeys.contains("input"));
assertFalse(r.supportedJnpfKeys.contains("unknownControl"));
// 3) usedJnpfKeys 含两者
assertEquals(2, r.usedJnpfKeys.size());
// 4) formSummary 标识 unsupported=1
assertTrue(r.formSummary.contains("unsupported=1"));
}
@Test
public void testColumnList() throws Exception {
// 构造 formData: columnList 含 input/datePicker 2 列
String formDataJson = "{\n" +
" \"formItems\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"}\n" +
" ],\n" +
" \"columnList\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"},\n" +
" {\"__config__\": {\"jnpfKey\": \"datePicker\"}, \"__vModel__\": \"f_date\"}\n" +
" ]\n" +
"}";
ReverseAnalysisReport r = new FormReverseDebugger().analyzeFormData(formDataJson);
// 1) columnListSize = 2
assertEquals(2, r.columnListSize);
// 2) usedJnpfKeys 包含 input + datePicker (columnList 也有)
assertTrue(r.usedJnpfKeys.contains("input"));
assertTrue(r.usedJnpfKeys.contains("datePicker"));
// 3) input 计数 = 2 (formItems 1 个 + columnList 1 个)
assertEquals(2, r.jnpfKeyCount.get("input").intValue());
assertEquals(1, r.jnpfKeyCount.get("datePicker").intValue());
// 4) totalFieldCount = 1 (只算 formItems)
assertEquals(1, r.totalFieldCount);
// 5) hasFlowTemplate = false
assertFalse(r.hasFlowTemplate);
// 6) formSummary 含 columns=2
assertTrue(r.formSummary.contains("columns=2"));
}
@Test
public void testDumpToFile() throws Exception {
// 写 target/baseline-dump/FormReverseDebugger.json 留作人类可读 baseline 调试样本
String formDataJson = "{\n" +
" \"formItems\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"},\n" +
" {\"__config__\": {\"jnpfKey\": \"relationForm\"}, \"__vModel__\": \"f_user\"}\n" +
" ],\n" +
" \"columnList\": [\n" +
" {\"__config__\": {\"jnpfKey\": \"input\"}, \"__vModel__\": \"f_name\"}\n" +
" ],\n" +
" \"flowTemplateJson\": null\n" +
"}";
ReverseAnalysisReport r = new FormReverseDebugger().analyzeFormData(formDataJson);
String reportJson = r.toJson();
Path out = Paths.get("target/baseline-dump/FormReverseDebugger.json");
Files.createDirectories(out.getParent());
Files.writeString(out, reportJson);
// 1) 文件存在
assertTrue(Files.exists(out));
// 2) 大小 > 100 字节 (JSON 报告不空)
assertTrue(Files.size(out) > 100);
// 3) 内容含关键字段
String content = Files.readString(out);
assertTrue(content.contains("usedJnpfKeys"), "报告应含 usedJnpfKeys 字段");
assertTrue(content.contains("\"input\""), "报告应含 input 控件");
assertTrue(content.contains("\"relationForm\""), "报告应含 relationForm 控件");
assertTrue(content.contains("totalFieldCount"), "报告应含 totalFieldCount 字段");
assertTrue(content.contains("columnListSize"), "报告应含 columnListSize 字段");
assertTrue(content.contains("hasFlowTemplate"), "报告应含 hasFlowTemplate 字段");
}
}