feat: 注解重命名(@PropertyName/@Ignore) + 新增 @NotRequired 默认必填语义, API 小写化 serialize/deserialize, 宏支持多注解组合, 版本号 1.2.0

This commit is contained in:
2026-08-25 11:59:13 +08:00
parent 1071d18a62
commit cd1b56fb81
12 changed files with 632 additions and 265 deletions
+8 -8
View File
@@ -183,7 +183,7 @@ import std.collection.*
public macro SerializerParent(input: Tokens): Tokens {
// 1. 解析输入的 ClassDecl(非类声明 → 抛 MacroException)
// 2. 校验 open:非 open class → 抛 MacroException(见 3.1.1)
// 3. 收集 public var 字段(跳过 @SerializerIgnore)
// 3. 收集 public var 字段(跳过 @Ignore)
// 4. 生成 exportJsonFields / importJsonFields 两个静态方法
// 5. 拼回原类声明 + 新方法,返回 Tokens
}
@@ -243,7 +243,7 @@ if (!isOpenClass(decl)) {
open class BaseUser {
public var _id: String = ""
public var _createdAt: String = ""
@SerializerIgnore
@Ignore
public var _temp: String = ""
}
```
@@ -254,7 +254,7 @@ open class BaseUser {
open class BaseUser {
public var _id: String = ""
public var _createdAt: String = ""
@SerializerIgnore
@Ignore
public var _temp: String = ""
/// 导出:字段名 → 值(静态类型访问)
@@ -262,7 +262,7 @@ open class BaseUser {
var m = HashMap<String, Any>()
m["_id"] = instance._id
m["_createdAt"] = instance._createdAt
// _temp 被 @SerializerIgnore 跳过
// _temp 被 @Ignore 跳过
m
}
@@ -318,8 +318,8 @@ Cangjie 宏需要两步编译(`cjc --compile-macro` 先编宏,再编使用
| 细节 | 处理 |
|---|---|
| 字段类型 | 用 `TypeInfo.get(字段类型名)` 在运行时做类型转换(import 侧) |
| `@SerializerPropertyName` | export 键用注解名;import 匹配也用注解名 |
| `@SerializerIgnore` | 直接跳过,不生成导出/导入语句 |
| `@PropertyName` | export 键用注解名;import 匹配也用注解名 |
| `@Ignore` | 直接跳过,不生成导出/导入语句 |
| `let`(不可变)字段 | 只导出,不导入(导入侧跳过) |
| 无字段 | 生成空方法(返回空 map / 空操作),保证约定方法一定存在 |
| 命名冲突 | 若用户已手写同名静态方法,宏应报错或跳过(设计取舍:报错更安全) |
@@ -474,7 +474,7 @@ main() {
| 用例 | 断言 |
|---|---|
| 父类字段序列化 | 输出包含 `_id`/`_createdAt`,且 `@SerializerIgnore` 父类字段不输出 |
| 父类字段序列化 | 输出包含 `_id`/`_createdAt`,且 `@Ignore` 父类字段不输出 |
| 父类字段反序列化 | `Deserialize<User>` 后父类字段值正确 |
| 多层继承 | GrandParent → Parent → Child 三层字段全部输出 |
| 中间层无宏 | 三层链中 Parent 不标宏:`_gp` 仍导出,`_p` 不导出(逐层跳过) |
@@ -482,7 +482,7 @@ main() {
| 子类字段与父类字段重名 | 子类优先(输出一份,值取子类的) |
| 父类嵌套对象字段 | 父类字段值是对象时正确递归 |
| 父类字段 null | 导入时保持默认值 |
| 与 @SerializerPropertyName 组合 | 父类字段用注解名输出/匹配 |
| 与 @PropertyName 组合 | 父类字段用注解名输出/匹配 |
| 顶层序列化父类类型实例 | `Serialize(BaseUser 实例)` 直接可用 |
| **宏自动补 open** | `@SerializerParent class Base`(无 open)展开后子类可继承(编译通过) |
| **宏严格模式报错** | `@SerializerParent[requireOpen: true]` 标注非 open class 编译失败 |