@deepstorm/cli 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -7285,6 +7285,14 @@ function copyFragmentsForSkill(skillId, srcDir, config, registry2, targetDir) {
7285
7285
  );
7286
7286
  }
7287
7287
  }
7288
+ const allFiles = fs10.readdirSync(srcPath);
7289
+ for (const file of allFiles) {
7290
+ if (file === ".DS_Store") continue;
7291
+ if (file === "quick-reference.md") continue;
7292
+ if (file === "examples") continue;
7293
+ if (!file.endsWith(".md")) continue;
7294
+ fs10.cpSync(path6.join(srcPath, file), path6.join(targetDir, file), { force: true });
7295
+ }
7288
7296
  }
7289
7297
  }
7290
7298
  function copyReferencesForSkill(srcDir, targetDir) {
@@ -122,6 +122,7 @@ deepstorm:
122
122
  | `quick-reference.md` | **编码规范速查**。包含核心规则和约定 |
123
123
  | `{value}.md` | **维度规范**(如 `spring-boot.md`、`hibernate.md`)。按上方链接加载 |
124
124
  | `api-spec.md` | **API 规范**。RESTful 命名、统一响应体、版本策略、OpenAPI |
125
+ | `jackson-polymorphism.md` | **DTO 多态序列化规范**。Jackson `@JsonTypeInfo` + TS Discriminated Union |
125
126
  | `dependency-management.md` | **依赖管理规范**。Version Catalog、版本一致性、CVE |
126
127
  | `exception-handling.md` | **异常处理深度规范**。异常层次、错误码、全局处理 |
127
128
  | `security-redlines.md` | **安全红线**。P0/P1 安全规则及代码示例 |
@@ -0,0 +1,205 @@
1
+ # Jackson 多态序列化与 TypeScript 联合类型
2
+
3
+ > 适用于后端枚举驱动的图表类型分发 → Jackson `@JsonTypeInfo` 多态 + 前端 discriminated union 的模式。
4
+ > 目标:删除后端 `enum` + `chartType` 字段,改用抽象基类 + `@JsonProperty("type")` 实现自动分发。
5
+
6
+ ```mermaid
7
+ flowchart LR
8
+ A["后端 Java<br/>@JsonTypeInfo(property = &quot;type&quot;)<br/>@JsonSubTypes"] -->|"JSON 携带<br/>type 判别值"| B["前端 TypeScript<br/>Discriminated Union"]
9
+ B --> C["@switch(answer.chartView.type)<br/>类型自动窄化"]
10
+ ```
11
+
12
+ ---
13
+
14
+ ## 一、问题场景
15
+
16
+ 后端接口返回 JSON 中包含一个"类型"字段,前端根据该类型渲染不同的组件:
17
+
18
+ ```java
19
+ // ❌ 旧模式:枚举 + switch
20
+ public record MetricAnswer(ChartType chartType, ...) {}
21
+ public enum ChartType { METRIC_CARD, LINE_CHART, BAR_CHART, PIE_CHART }
22
+
23
+ // 前端每次新增类型都需要:
24
+ // 1. 加 enum 值
25
+ // 2. 改 switch
26
+ // 3. 不在同一个地方维护
27
+ ```
28
+
29
+ ## 二、推荐模式:Jackson 多态序列化
30
+
31
+ ### 2.1 后端
32
+
33
+ **步骤 1:定义抽象基类**
34
+
35
+ ```java
36
+ @JsonTypeInfo(use = JsonTypeInfo.Id.SIMPLE_NAME, property = "type")
37
+ @JsonSubTypes({
38
+ @JsonSubTypes.Type(value = MetricCardView.class, name = "MetricCardView"),
39
+ @JsonSubTypes.Type(value = LineChartView.class, name = "LineChartView"),
40
+ @JsonSubTypes.Type(value = BarChartView.class, name = "BarChartView"),
41
+ @JsonSubTypes.Type(value = PieChartView.class, name = "PieChartView"),
42
+ })
43
+ public abstract class ChartView {}
44
+ ```
45
+
46
+ - `property = "type"` — 序列化时自动在 JSON 中写入 `"type": "MetricCardView"` 等 discriminator
47
+ - `name = "..."` — discriminator 的值,应与 TypeScript 端 literal type 保持一致
48
+ - 新增类型只需:新建子类 + 注册到 `@JsonSubTypes`
49
+
50
+ **步骤 2:定义子类**
51
+
52
+ ```java
53
+ @JsonInclude(Include.NON_NULL)
54
+ public class MetricCardView extends ChartView {
55
+ private final String title;
56
+ private final Object value;
57
+ private final String unit;
58
+
59
+ public MetricCardView(String title, Object value, String unit) {
60
+ this.title = title;
61
+ this.value = value;
62
+ this.unit = unit;
63
+ }
64
+ // getters ...
65
+ }
66
+ ```
67
+
68
+ ```java
69
+ public class LineChartView extends ChartView {
70
+ private final String title;
71
+ @JsonProperty("xAxisName") private final String categoryAxisLabel;
72
+ @JsonProperty("yAxisName") private final String valueAxisLabel;
73
+ private final String seriesName;
74
+ private final List<ChartDataPoint> dataPoints;
75
+ // constructor + getters ...
76
+ }
77
+ ```
78
+
79
+ > **💡 Import 说明:**
80
+ > - `@JsonTypeInfo`、`@JsonSubTypes`、`@JsonInclude` 来自 `com.fasterxml.jackson.annotation.*`
81
+ > - `@JsonInclude(Include.NON_NULL)` 需导入 `com.fasterxml.jackson.annotation.JsonInclude.Include`
82
+ > - 或在注解中写全路径 `@JsonInclude(JsonInclude.Include.NON_NULL)`,无需额外 import
83
+
84
+ **步骤 3:替换枚举字段**
85
+
86
+ ```java
87
+ // ❌ 旧
88
+ public record MetricAnswer(ChartType chartType, ...) {}
89
+
90
+ // ✅ 新
91
+ public record MetricAnswer(ChartView chartView, ...) {}
92
+ ```
93
+
94
+ - 业务逻辑构建时直接 `return new PieChartView(...)` 而非 `return ChartType.PIE_CHART`
95
+ - 删除 `ChartType` 枚举文件
96
+
97
+ ### 2.2 前端(TypeScript)
98
+
99
+ **步骤 1:定义 Discriminated Union**
100
+
101
+ ```typescript
102
+ interface BaseChartView {
103
+ type: string;
104
+ }
105
+
106
+ export interface MetricCardView extends BaseChartView {
107
+ type: 'MetricCardView';
108
+ title: string;
109
+ value: unknown;
110
+ unit: string | null;
111
+ }
112
+
113
+ export interface LineChartView extends BaseChartView {
114
+ type: 'LineChartView';
115
+ title: string;
116
+ xAxisName: string;
117
+ yAxisName: string;
118
+ seriesName: string;
119
+ dataPoints: ChartDataPoint[];
120
+ }
121
+
122
+ export type ChartView = MetricCardView | LineChartView | BarChartView | PieChartView;
123
+ ```
124
+
125
+ **步骤 2:MetricAnswer 中使用联合类型**
126
+
127
+ ```typescript
128
+ export interface MetricAnswer {
129
+ // ...
130
+ chartView: ChartView | null; // ✅ 联合类型自动窄化
131
+ }
132
+ ```
133
+
134
+ **步骤 3:模板中使用 @switch**
135
+
136
+ ```html
137
+ @if (answer.chartView) {
138
+ @switch (answer.chartView.type) {
139
+ @case ('MetricCardView') {
140
+ <app-metric-card [answer]="answer" />
141
+ }
142
+ @case ('LineChartView') {
143
+ <app-line-chart [chartData]="answer.chartData" />
144
+ }
145
+ }
146
+ }
147
+ ```
148
+
149
+ ## 三、最佳实践
150
+
151
+ | 要点 | 说明 |
152
+ |------|------|
153
+ | `property = "type"` | discriminator 字段名使用 `"type"`(简短、通用),而非 `"chartType"` |
154
+ | 前后端 value 一致 | `@JsonSubTypes.Type(name = "MetricCardView")` 与 TS `type: 'MetricCardView'` 的值必须一致 |
155
+ | Checkstyle 兼容 | Jackson 字段名与 getter 名可以不同:`@JsonProperty("xAxisName") private String categoryAxisLabel` |
156
+ | 共享 DTO | 子类共享的嵌套类型(如 `ChartDataPoint`)定义为独立 `record` 或类 |
157
+ | 判空安全 | 模板中 `@if (answer.chartView)` 保护空值(如错误响应时 chartView 为 null) |
158
+ | 不删除旧 DTO 层 | `ChartData`、`gridResults`、`singleResults` 等依然存在,chartView 是新增的视图抽象,并非替代所有响应字段 |
159
+ | 删除前确认引用 | 删除旧枚举前 MUST 搜索全项目 Java + TypeScript 引用,确认 0 引用后方可删除 |
160
+
161
+ ## 四、效果对比
162
+
163
+ | 维度 | 旧模式(Enum) | 新模式(Polymorphism) |
164
+ |------|---------------|----------------------|
165
+ | 新增图表类型 | 枚举 + switch + 前端分支 | 新建子类 + 注册 `@JsonSubTypes` + TS 类型 |
166
+ | JSON 结构 | `{"chartType": "METRIC_CARD", ...}` | `{"chartView": {"type": "MetricCardView", ...}}` |
167
+ | TS 类型安全 | 手动维护 | 自动通过 discriminated union 窄化 |
168
+ | 后端字段数 | chartType + 具体数据各自校验 | chartView 自包含 |
169
+
170
+ ## 五、设计决策说明
171
+
172
+ ### 5.1 `property` 选择 `"type"` 而非 `"chartType"`
173
+
174
+ | 候选 | 理由 | 结论 |
175
+ |------|------|------|
176
+ | `"type"` | 简短通用,多态基类职责就是描述"是什么类型",与低代码项目 (`DatasetDto`) 一致 | ✅ |
177
+ | `"chartType"` | 业务含义明确,但限制了基类的可复用性。未来其他多态场景(如 `DataSourceConnector`)需要不同字段名 | ❌ |
178
+ | `"kind"` | Jackson 社区惯例使用 `"type"`,不应引入不必要的差异 | ❌ |
179
+
180
+ ### 5.2 discriminator 策略使用 `SIMPLE_NAME`
181
+
182
+ | 候选 | 理由 | 结论 |
183
+ |------|------|------|
184
+ | `SIMPLE_NAME` | 子类简名自描述,无需额外常量文件 | ✅ |
185
+ | `CLASS_NAME` | 全限定名过长,JSON 中不可读且暴露包结构 | ❌ |
186
+ | `CUSTOM` | 需要 `@JsonTypeIdResolver`,增加复杂度 | ❌ |
187
+
188
+ ### 5.3 使用 `@JsonSubTypes` 而非自定义序列化器
189
+
190
+ `@JsonSubTypes` 是 Jackson 原生支持,注解式声明最直观。`@JsonTypeIdResolver` 或自定义 `JsonSerializer` 提供更灵活的控制但需要大量模板代码,适用于更复杂的动态注册场景而非当前已知类型场景。
191
+
192
+ ### 5.4 前端 Discriminated Union 而非 Enum
193
+
194
+ TypeScript union type + `type` literal 可直接利用 `@switch` 的类型窄化能力,无需手动编写类型守卫函数。这与后端 enum 模式的维护成本形成鲜明对比。
195
+
196
+ ### 5.5 Checkstyle 冲突:`@JsonProperty` 解耦而非禁用规则
197
+
198
+ | 候选 | 理由 | 结论 |
199
+ |------|------|------|
200
+ | `@JsonProperty("xAxisName") private String categoryAxisLabel` | 字段名合规,getter 合规,JSON 输出保留 `xAxisName` | ✅ |
201
+ | 禁用 Checkstyle `ParameterNameCheck` | 全局禁用降低代码规范性 | ❌ |
202
+
203
+ ### 5.6 Decoder 对象保持 record
204
+
205
+ 如 `MetricAnswer` 是 Java record,不能继承。改造方案为直接替换字段类型(`ChartType chartType` → `ChartView chartView`),record 紧凑语法不受影响。构建逻辑从 `determineChartType()` 改为 `buildChartView()` 并返回具体子类。
@@ -146,3 +146,11 @@ MODULE: 2-4 个大写字母标识模块
146
146
  - `_100`-`_199`:资源状态错误(不存在、已存在、冲突)
147
147
  - `_200`-`_299`:权限错误
148
148
  - `_300`-`_399`:系统内部错误
149
+
150
+ ## DTO 多态序列化
151
+
152
+ 当接口中的 DTO 需要根据类型字段分发到不同子类时(如多种图表类型、多种数据源类型),参考 [DTO 多态序列化规范](jackson-polymorphism.md):
153
+
154
+ - 后端使用 Jackson `@JsonTypeInfo` + `@JsonSubTypes` 实现多态序列化
155
+ - 前端使用 TypeScript Discriminated Union 对应消费
156
+ - discriminator value 前后端严格一致
@@ -62,6 +62,20 @@
62
62
  }
63
63
  ```
64
64
 
65
+ ### Lombok 使用规范
66
+
67
+ | 组件类型 | 必用注解 | 说明 |
68
+ |---------|---------|------|
69
+ | 领域事件 / POJO | `@Getter @AllArgsConstructor` | `private final` 不可变,不手写 getter/constructor(见下方示例) |
70
+ | JPA Entity | `@Getter @NoArgsConstructor(access = PROTECTED)` + `@SuperBuilder`(按需) | 详见 [Hibernate 规范](hibernate.md) |
71
+ | Service / Controller | `@AllArgsConstructor` / `@RequiredArgsConstructor` | 构造函数注入,不用 `@Autowired` 字段注入 |
72
+ | 只读 DTO / Record 替代 | `@Value` | 不可变对象,自动生成 equals/hashCode/toString。Record 更简洁时优先用 record |
73
+
74
+ **禁止事项:**
75
+ - `@Data` — 自动生成 `@Setter` 破坏不可变性,且 `@EqualsAndHashCode` 在有 JPA 代理时行为不可预期
76
+ - `@Setter` on Entity — 破坏封装,业务状态变更应通过行为方法表达
77
+ - `@Autowired` 字段注入 — 必须用构造函数注入
78
+
65
79
  ### 控件能力声明模式(Capability Pattern)
66
80
 
67
81
  当实体层次(如 `FormControl` → `TextControl` / `NumberControl` / ...)需要按子类暴露能力时,在抽象基类中声明抽象方法,各子类返回 `true` / `false`:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deepstorm/cli",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "DeepStorm CLI — 一键配置项目开发环境",
5
5
  "license": "MIT",
6
6
  "author": "billkang",