@hadss/turbo-trans-json-plugin 1.0.0-rc.0
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/LICENSE +177 -0
- package/README.md +253 -0
- package/dist/core/Types.d.ts +196 -0
- package/dist/core/Types.js +47 -0
- package/dist/core/analyzers/ClassAnalyzer.d.ts +23 -0
- package/dist/core/analyzers/ClassAnalyzer.js +480 -0
- package/dist/core/analyzers/CustomTypeAnalyzer.d.ts +21 -0
- package/dist/core/analyzers/CustomTypeAnalyzer.js +243 -0
- package/dist/core/analyzers/SendableMergeChecker.d.ts +9 -0
- package/dist/core/analyzers/SendableMergeChecker.js +100 -0
- package/dist/core/analyzers/SendableMergeabilityRegistry.d.ts +7 -0
- package/dist/core/analyzers/SendableMergeabilityRegistry.js +23 -0
- package/dist/core/constants/DecoratorConstants.d.ts +7 -0
- package/dist/core/constants/DecoratorConstants.js +13 -0
- package/dist/core/constants/PathConstants.d.ts +6 -0
- package/dist/core/constants/PathConstants.js +10 -0
- package/dist/core/constants/StringConstants.d.ts +83 -0
- package/dist/core/constants/StringConstants.js +87 -0
- package/dist/core/constants/index.d.ts +3 -0
- package/dist/core/constants/index.js +19 -0
- package/dist/core/handlers/BaseTypeHandler.d.ts +23 -0
- package/dist/core/handlers/BaseTypeHandler.js +57 -0
- package/dist/core/handlers/CustomClassHandler.d.ts +21 -0
- package/dist/core/handlers/CustomClassHandler.js +191 -0
- package/dist/core/handlers/DateHandler.d.ts +2 -0
- package/dist/core/handlers/DateHandler.js +60 -0
- package/dist/core/handlers/DecimalHandler.d.ts +2 -0
- package/dist/core/handlers/DecimalHandler.js +60 -0
- package/dist/core/handlers/EnumHandler.d.ts +2 -0
- package/dist/core/handlers/EnumHandler.js +89 -0
- package/dist/core/handlers/GenericContainerHandler.d.ts +2 -0
- package/dist/core/handlers/GenericContainerHandler.js +440 -0
- package/dist/core/handlers/GenericHandler.d.ts +18 -0
- package/dist/core/handlers/GenericHandler.js +92 -0
- package/dist/core/handlers/HandlerBootstrap.d.ts +2 -0
- package/dist/core/handlers/HandlerBootstrap.js +28 -0
- package/dist/core/handlers/ITypeHandler.d.ts +23 -0
- package/dist/core/handlers/ITypeHandler.js +8 -0
- package/dist/core/handlers/PrimitiveHandler.d.ts +2 -0
- package/dist/core/handlers/PrimitiveHandler.js +127 -0
- package/dist/core/handlers/TupleHandler.d.ts +2 -0
- package/dist/core/handlers/TupleHandler.js +98 -0
- package/dist/core/handlers/TypeHandlerRegistry.d.ts +20 -0
- package/dist/core/handlers/TypeHandlerRegistry.js +113 -0
- package/dist/core/handlers/UnionTypeHandler.d.ts +2 -0
- package/dist/core/handlers/UnionTypeHandler.js +263 -0
- package/dist/core/handlers/index.d.ts +2 -0
- package/dist/core/handlers/index.js +5 -0
- package/dist/core/import-rewrite/services/BuildProfileUpdater.d.ts +8 -0
- package/dist/core/import-rewrite/services/BuildProfileUpdater.js +92 -0
- package/dist/core/import-rewrite/services/ImportRewriteService.d.ts +9 -0
- package/dist/core/import-rewrite/services/ImportRewriteService.js +61 -0
- package/dist/core/import-rewrite/services/ImportTransformService.d.ts +15 -0
- package/dist/core/import-rewrite/services/ImportTransformService.js +109 -0
- package/dist/core/import-rewrite/strategies/ImportScanStrategy.d.ts +17 -0
- package/dist/core/import-rewrite/strategies/ImportScanStrategy.js +137 -0
- package/dist/core/import-rewrite/types/ImportRewriteTypes.d.ts +17 -0
- package/dist/core/import-rewrite/types/ImportRewriteTypes.js +2 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +27 -0
- package/dist/core/interfaces/ITask.d.ts +7 -0
- package/dist/core/interfaces/ITask.js +2 -0
- package/dist/core/interfaces/ITaskContext.d.ts +11 -0
- package/dist/core/interfaces/ITaskContext.js +2 -0
- package/dist/core/interfaces/index.d.ts +2 -0
- package/dist/core/interfaces/index.js +2 -0
- package/dist/core/logger/Logger.d.ts +13 -0
- package/dist/core/logger/Logger.js +78 -0
- package/dist/core/services/CodeAnalysisService.d.ts +7 -0
- package/dist/core/services/CodeAnalysisService.js +43 -0
- package/dist/core/services/CodeGenerationEngine.d.ts +12 -0
- package/dist/core/services/CodeGenerationEngine.js +102 -0
- package/dist/core/services/CodeGenerationService/CodeGenerationService.d.ts +13 -0
- package/dist/core/services/CodeGenerationService/CodeGenerationService.js +110 -0
- package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.d.ts +13 -0
- package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.js +119 -0
- package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.d.ts +21 -0
- package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.js +224 -0
- package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.d.ts +24 -0
- package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.js +307 -0
- package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.d.ts +28 -0
- package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.js +259 -0
- package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.d.ts +19 -0
- package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.js +178 -0
- package/dist/core/services/CodeGenerationService/index.d.ts +1 -0
- package/dist/core/services/CodeGenerationService/index.js +5 -0
- package/dist/core/services/CodeGenerationService/shared/ImportManager.d.ts +27 -0
- package/dist/core/services/CodeGenerationService/shared/ImportManager.js +127 -0
- package/dist/core/template/HandlebarsTemplateEngine.d.ts +26 -0
- package/dist/core/template/HandlebarsTemplateEngine.js +226 -0
- package/dist/core/utils/ConfigManager.d.ts +10 -0
- package/dist/core/utils/ConfigManager.js +32 -0
- package/dist/core/utils/CustomError.d.ts +30 -0
- package/dist/core/utils/CustomError.js +37 -0
- package/dist/core/utils/DeepCopyUtil.d.ts +15 -0
- package/dist/core/utils/DeepCopyUtil.js +138 -0
- package/dist/core/utils/GenericTypeSubstitutionUtil.d.ts +9 -0
- package/dist/core/utils/GenericTypeSubstitutionUtil.js +68 -0
- package/dist/core/utils/SerializationPathUtil.d.ts +18 -0
- package/dist/core/utils/SerializationPathUtil.js +107 -0
- package/dist/core/utils/TsMorphUtil.d.ts +8 -0
- package/dist/core/utils/TsMorphUtil.js +34 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +57 -0
- package/dist/json-plugin/JSONExecuteController.d.ts +13 -0
- package/dist/json-plugin/JSONExecuteController.js +103 -0
- package/dist/json-plugin/constants/TaskConstants.d.ts +7 -0
- package/dist/json-plugin/constants/TaskConstants.js +11 -0
- package/dist/json-plugin/interfaces/IModuleContext.d.ts +10 -0
- package/dist/json-plugin/interfaces/IModuleContext.js +2 -0
- package/dist/json-plugin/interfaces/ITargetContext.d.ts +8 -0
- package/dist/json-plugin/interfaces/ITargetContext.js +2 -0
- package/dist/json-plugin/interfaces/impl/ModuleContext.d.ts +12 -0
- package/dist/json-plugin/interfaces/impl/ModuleContext.js +24 -0
- package/dist/json-plugin/interfaces/impl/TargetContext.d.ts +23 -0
- package/dist/json-plugin/interfaces/impl/TargetContext.js +113 -0
- package/dist/json-plugin/tasks/BaseTask.d.ts +14 -0
- package/dist/json-plugin/tasks/BaseTask.js +53 -0
- package/dist/json-plugin/tasks/CleanTask.d.ts +8 -0
- package/dist/json-plugin/tasks/CleanTask.js +25 -0
- package/dist/json-plugin/tasks/CodeProcessingTask.d.ts +8 -0
- package/dist/json-plugin/tasks/CodeProcessingTask.js +26 -0
- package/dist/json-plugin/tasks/SyncTask.d.ts +8 -0
- package/dist/json-plugin/tasks/SyncTask.js +21 -0
- package/dist/json-plugin/tasks/WatchTask.d.ts +11 -0
- package/dist/json-plugin/tasks/WatchTask.js +102 -0
- package/package.json +46 -0
- package/src/core/Types.ts +356 -0
- package/src/core/analyzers/ClassAnalyzer.ts +824 -0
- package/src/core/analyzers/CustomTypeAnalyzer.ts +337 -0
- package/src/core/analyzers/SendableMergeChecker.ts +195 -0
- package/src/core/analyzers/SendableMergeabilityRegistry.ts +72 -0
- package/src/core/constants/DecoratorConstants.ts +27 -0
- package/src/core/constants/PathConstants.ts +31 -0
- package/src/core/constants/StringConstants.ts +152 -0
- package/src/core/constants/index.ts +21 -0
- package/src/core/handlers/BaseTypeHandler.ts +121 -0
- package/src/core/handlers/CustomClassHandler.ts +278 -0
- package/src/core/handlers/DateHandler.ts +75 -0
- package/src/core/handlers/DecimalHandler.ts +75 -0
- package/src/core/handlers/EnumHandler.ts +142 -0
- package/src/core/handlers/GenericContainerHandler.ts +621 -0
- package/src/core/handlers/GenericHandler.ts +130 -0
- package/src/core/handlers/HandlerBootstrap.ts +64 -0
- package/src/core/handlers/ITypeHandler.ts +133 -0
- package/src/core/handlers/PrimitiveHandler.ts +159 -0
- package/src/core/handlers/TupleHandler.ts +145 -0
- package/src/core/handlers/TypeHandlerRegistry.ts +236 -0
- package/src/core/handlers/UnionTypeHandler.ts +400 -0
- package/src/core/handlers/index.ts +18 -0
- package/src/core/import-rewrite/services/BuildProfileUpdater.ts +145 -0
- package/src/core/import-rewrite/services/ImportRewriteService.ts +129 -0
- package/src/core/import-rewrite/services/ImportTransformService.ts +200 -0
- package/src/core/import-rewrite/strategies/ImportScanStrategy.ts +303 -0
- package/src/core/import-rewrite/types/ImportRewriteTypes.ts +100 -0
- package/src/core/index.ts +31 -0
- package/src/core/interfaces/ITask.ts +23 -0
- package/src/core/interfaces/ITaskContext.ts +94 -0
- package/src/core/interfaces/index.ts +17 -0
- package/src/core/logger/Logger.ts +149 -0
- package/src/core/services/CodeAnalysisService.ts +67 -0
- package/src/core/services/CodeGenerationEngine.ts +181 -0
- package/src/core/services/CodeGenerationService/CodeGenerationService.ts +159 -0
- package/src/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.ts +189 -0
- package/src/core/services/CodeGenerationService/generators/OriginalClassGenerator.ts +314 -0
- package/src/core/services/CodeGenerationService/generators/SendableClassGenerator.ts +421 -0
- package/src/core/services/CodeGenerationService/generators/SerializerGenerator.ts +392 -0
- package/src/core/services/CodeGenerationService/generators/TempSerializerGenerator.ts +277 -0
- package/src/core/services/CodeGenerationService/index.ts +16 -0
- package/src/core/services/CodeGenerationService/shared/ImportManager.ts +191 -0
- package/src/core/template/HandlebarsTemplateEngine.ts +282 -0
- package/src/core/utils/ConfigManager.ts +49 -0
- package/src/core/utils/CustomError.ts +51 -0
- package/src/core/utils/DeepCopyUtil.ts +185 -0
- package/src/core/utils/GenericTypeSubstitutionUtil.ts +136 -0
- package/src/core/utils/SerializationPathUtil.ts +142 -0
- package/src/core/utils/TsMorphUtil.ts +50 -0
- package/src/index.ts +81 -0
- package/src/json-plugin/JSONExecuteController.ts +134 -0
- package/src/json-plugin/constants/TaskConstants.ts +22 -0
- package/src/json-plugin/interfaces/IModuleContext.ts +27 -0
- package/src/json-plugin/interfaces/ITargetContext.ts +25 -0
- package/src/json-plugin/interfaces/impl/ModuleContext.ts +45 -0
- package/src/json-plugin/interfaces/impl/TargetContext.ts +196 -0
- package/src/json-plugin/tasks/BaseTask.ts +94 -0
- package/src/json-plugin/tasks/CleanTask.ts +36 -0
- package/src/json-plugin/tasks/CodeProcessingTask.ts +41 -0
- package/src/json-plugin/tasks/SyncTask.ts +33 -0
- package/src/json-plugin/tasks/WatchTask.ts +99 -0
- package/template/SerializerPerformanceTemplate.hbs +35 -0
- package/template/SerializerRegisterTemplate.hbs +10 -0
- package/template/SerializerStrictTemplate.hbs +89 -0
- package/template/SerializerTemplate.hbs +176 -0
- package/tsconfig.json +17 -0
- package/tslint.json +3 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { PropertyKind } from '../Types';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 字符串常量统一管理
|
|
20
|
+
* 避免硬编码字符串散布在代码中
|
|
21
|
+
*/
|
|
22
|
+
export class StringConstants {
|
|
23
|
+
|
|
24
|
+
// ========== 基础类型字符串 ==========
|
|
25
|
+
static readonly TYPE_BOOLEAN = 'boolean'
|
|
26
|
+
static readonly TYPE_NUMBER = 'number'
|
|
27
|
+
static readonly TYPE_STRING = 'string'
|
|
28
|
+
static readonly TYPE_BIGINT = 'bigint'
|
|
29
|
+
static readonly TYPE_NULL = 'null'
|
|
30
|
+
static readonly TYPE_UNDEFINED = 'undefined'
|
|
31
|
+
static readonly TYPE_ARRAY = 'array'
|
|
32
|
+
static readonly TYPE_SET = 'set'
|
|
33
|
+
static readonly TYPE_MAP = 'map'
|
|
34
|
+
static readonly TYPE_UNION = 'union'
|
|
35
|
+
static readonly TYPE_ENUM = 'enum'
|
|
36
|
+
|
|
37
|
+
// ========== 序列化器名称 ==========
|
|
38
|
+
// 基础类型序列化器(单例INSTANCE模式)
|
|
39
|
+
static readonly BOOLEAN_SERIALIZER = 'BooleanSerializer'
|
|
40
|
+
static readonly NUMBER_SERIALIZER = 'NumberSerializer'
|
|
41
|
+
static readonly STRING_SERIALIZER = 'StringSerializer'
|
|
42
|
+
static readonly BIGINT_SERIALIZER = 'BigintSerializer'
|
|
43
|
+
|
|
44
|
+
// 特殊原始类型序列化器(单例INSTANCE模式)
|
|
45
|
+
static readonly NULL_SERIALIZER = 'NullSerializer'
|
|
46
|
+
static readonly UNDEFINED_SERIALIZER = 'UndefinedSerializer'
|
|
47
|
+
|
|
48
|
+
// 集合类型序列化器(泛型构造器模式)
|
|
49
|
+
static readonly ARRAY_SERIALIZER = 'ArraySerializer'
|
|
50
|
+
static readonly COLLECTIONS_ARRAY_SERIALIZER = 'CollectionsArraySerializer'
|
|
51
|
+
static readonly MAP_SERIALIZER = 'MapSerializer'
|
|
52
|
+
static readonly COLLECTIONS_MAP_SERIALIZER = 'CollectionsMapSerializer'
|
|
53
|
+
static readonly SET_SERIALIZER = 'SetSerializer'
|
|
54
|
+
static readonly COLLECTIONS_SET_SERIALIZER = 'CollectionsSetSerializer'
|
|
55
|
+
static readonly RECORD_SERIALIZER = 'RecordSerializer'
|
|
56
|
+
|
|
57
|
+
// 高级类型序列化器(特殊构造器模式)
|
|
58
|
+
static readonly UNION_TYPE_SERIALIZER = 'UnionTypeSerializer'
|
|
59
|
+
static readonly OPTIONAL_SERIALIZER = 'OptionalSerializer'
|
|
60
|
+
static readonly NULLABLE_SERIALIZER = 'NullableSerializer'
|
|
61
|
+
static readonly ENUM_SERIALIZER = 'EnumSerializer'
|
|
62
|
+
|
|
63
|
+
// 通用序列化器
|
|
64
|
+
static readonly SERIALIZER = 'Serializer'
|
|
65
|
+
static readonly SERIALIZER_FACTORY = 'SerializerFactory'
|
|
66
|
+
|
|
67
|
+
// ========== 编码/解码方法名 ==========
|
|
68
|
+
// 基础类型专用编码方法
|
|
69
|
+
static readonly ENCODE_BOOLEAN_ELEMENT = 'encodeBooleanElement'
|
|
70
|
+
static readonly ENCODE_NUMBER_ELEMENT = 'encodeNumberElement'
|
|
71
|
+
static readonly ENCODE_STRING_ELEMENT = 'encodeStringElement'
|
|
72
|
+
static readonly ENCODE_BIGINT_ELEMENT = 'encodeBigintElement'
|
|
73
|
+
|
|
74
|
+
// 特殊原始类型专用编码方法
|
|
75
|
+
static readonly ENCODE_NULL_ELEMENT = 'encodeNullElement'
|
|
76
|
+
static readonly ENCODE_UNDEFINED_ELEMENT = 'encodeUndefinedElement'
|
|
77
|
+
|
|
78
|
+
// 通用编码方法
|
|
79
|
+
static readonly ENCODE_SERIALIZABLE_ELEMENT = 'encodeSerializableElement'
|
|
80
|
+
|
|
81
|
+
// 基础类型专用解码方法
|
|
82
|
+
static readonly DECODE_BOOLEAN = 'decodeBoolean'
|
|
83
|
+
static readonly DECODE_NUMBER = 'decodeNumber'
|
|
84
|
+
static readonly DECODE_STRING = 'decodeString'
|
|
85
|
+
static readonly DECODE_BIGINT = 'decodeBigint'
|
|
86
|
+
|
|
87
|
+
// 特殊原始类型专用解码方法
|
|
88
|
+
static readonly DECODE_NULL = 'decodeNull'
|
|
89
|
+
static readonly DECODE_UNDEFINED = 'decodeUndefined'
|
|
90
|
+
|
|
91
|
+
// 通用解码方法
|
|
92
|
+
static readonly DESERIALIZABLE = 'deserialize'
|
|
93
|
+
|
|
94
|
+
// ========== 导入模块相关 ==========
|
|
95
|
+
static readonly IMPORT_BUILD_CLASS_SERIAL_DESCRIPTOR = 'buildClassSerialDescriptor'
|
|
96
|
+
static readonly IMPORT_DECODER = 'Decoder'
|
|
97
|
+
static readonly IMPORT_ENCODER = 'Encoder'
|
|
98
|
+
static readonly IMPORT_SERIAL_DESCRIPTOR = 'SerialDescriptor'
|
|
99
|
+
static readonly IMPORT_SERIALIZER = 'Serializer'
|
|
100
|
+
|
|
101
|
+
static readonly IMPORT_TJSON = 'TJSON'
|
|
102
|
+
static readonly IMPORT_IT_SERIALIZABLE = 'ITSerializable'
|
|
103
|
+
static readonly IMPORT_LANG = 'lang'
|
|
104
|
+
|
|
105
|
+
// ========== 类型推断相关 ==========
|
|
106
|
+
static readonly BOOLEAN_WRAPPER = 'Boolean'
|
|
107
|
+
static readonly NUMBER_WRAPPER = 'Number'
|
|
108
|
+
static readonly STRING_WRAPPER = 'String'
|
|
109
|
+
|
|
110
|
+
static readonly ARRAY_SUFFIX = '[]'
|
|
111
|
+
static readonly ARRAY_PREFIX = 'array<'
|
|
112
|
+
|
|
113
|
+
// ========== 默认值相关 ==========
|
|
114
|
+
static readonly DEFAULT_VALUE_FALSE = 'false'
|
|
115
|
+
static readonly DEFAULT_VALUE_ZERO = '0'
|
|
116
|
+
static readonly DEFAULT_VALUE_EMPTY_STRING = "''"
|
|
117
|
+
static readonly DEFAULT_VALUE_ZERO_BIGINT = '0n'
|
|
118
|
+
static readonly DEFAULT_VALUE_UNDEFINED = 'undefined'
|
|
119
|
+
|
|
120
|
+
// ========== 模板字符串片段 ==========
|
|
121
|
+
static readonly TEMPLATE_ELEMENT_NAME = 'element'
|
|
122
|
+
static readonly TEMPLATE_SENDABLE_ERROR_PREFIX = 'Sendable support not enabled for'
|
|
123
|
+
|
|
124
|
+
// ========== 文件操作相关 ==========
|
|
125
|
+
static readonly FILE_OPERATION_EMPTY_STRING = ''
|
|
126
|
+
static readonly REGEX_IMPORT_STATEMENT = /import\s+({[^}]+}|\w+)\s+from\s+'([^']+)'/
|
|
127
|
+
static readonly REGEX_SRC_MAIN_ETS = /\/src\/main\/ets\/(.*)\.ets$/
|
|
128
|
+
|
|
129
|
+
// ========== 分隔符和连接符 ==========
|
|
130
|
+
static readonly SEPARATOR_COMMA = ','
|
|
131
|
+
static readonly SEPARATOR_COMMA_NEWLINE = ',\n'
|
|
132
|
+
static readonly SEPARATOR_NEWLINE = '\n'
|
|
133
|
+
static readonly SEPARATOR_COMMA_SPACE = ', '
|
|
134
|
+
static readonly JOIN_ELEMENT_PREFIX = ',\n '
|
|
135
|
+
|
|
136
|
+
// ========== 正则表达式字符串 ==========
|
|
137
|
+
static readonly CURLY_BRACE_START = '{'
|
|
138
|
+
static readonly PATH_RELATIVE_PREFIX = './'
|
|
139
|
+
static readonly INSTANCE_SUFFIX = '.INSTANCE'
|
|
140
|
+
static readonly SERIALIZER_SUFFIX = 'Serializer'
|
|
141
|
+
|
|
142
|
+
// ========== 日志前缀 ==========
|
|
143
|
+
static readonly LOG_PREFIX_T_SERIALIZATION = '[TSerialization]'
|
|
144
|
+
static readonly LOG_MODULE_PREFIX_TEMPLATE = '[${module}]'
|
|
145
|
+
|
|
146
|
+
// ========== 插件标识 ==========
|
|
147
|
+
static readonly PLUGIN_ID = 'turboTransJsonPlugin'
|
|
148
|
+
|
|
149
|
+
// ========== 模块标识符 ==========
|
|
150
|
+
static readonly JSON_MODULE_SPECIFIER = '@hadss/hmserialization-json'
|
|
151
|
+
static readonly ARKTS_MODULE_SPECIFIER = '@kit.ArkTS'
|
|
152
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Core常量统一导出
|
|
18
|
+
*/
|
|
19
|
+
export * from './StringConstants';
|
|
20
|
+
export * from './DecoratorConstants';
|
|
21
|
+
export * from './PathConstants';
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { TypeNode } from 'ts-morph';
|
|
17
|
+
import { ITypeHandler, TypeContext, ConversionDirection } from './ITypeHandler';
|
|
18
|
+
import { PropertyKind, SerializerMetadata, TypeStructure } from '../Types';
|
|
19
|
+
|
|
20
|
+
export abstract class BaseTypeHandler implements ITypeHandler {
|
|
21
|
+
abstract readonly name: string;
|
|
22
|
+
abstract readonly kind: PropertyKind;
|
|
23
|
+
abstract readonly priority: number;
|
|
24
|
+
|
|
25
|
+
// 子类必须实现的所有核心方法
|
|
26
|
+
abstract matches(typeNode: TypeNode, context: TypeContext): boolean;
|
|
27
|
+
abstract parse(typeNode: TypeNode, context: TypeContext): TypeStructure;
|
|
28
|
+
abstract getSerializerMetadata(structure: TypeStructure, withParam?: string): SerializerMetadata;
|
|
29
|
+
abstract generateInstantiation(structure: TypeStructure): string;
|
|
30
|
+
abstract generateTypeDeclaration(structure: TypeStructure): string;
|
|
31
|
+
abstract generateTypeKey(structure: TypeStructure): string;
|
|
32
|
+
abstract generateSendableTypeDeclaration(structure: TypeStructure): string;
|
|
33
|
+
abstract generatePropertyConversion(structure: TypeStructure, sourceValue: string, direction: ConversionDirection): string;
|
|
34
|
+
|
|
35
|
+
/** 获取序列化器名称(约定:TypeName + Serializer)*/
|
|
36
|
+
protected getSerializerName(): string {
|
|
37
|
+
return `${this.getTypeName()}Serializer`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 获取类型名称 */
|
|
41
|
+
protected getTypeName(): string {
|
|
42
|
+
return this.capitalize(this.kind);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 首字母大写 */
|
|
46
|
+
protected capitalize(str: string): string {
|
|
47
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ==================== With参数处理 ====================
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 应用with参数覆盖默认的序列化器实例化代码
|
|
54
|
+
* @param withParam 自定义序列化器表达式
|
|
55
|
+
* @param defaultInstantiation 默认实例化代码
|
|
56
|
+
* @param structure 类型结构
|
|
57
|
+
*/
|
|
58
|
+
protected applyWithParam(withParam: string | undefined, defaultInstantiation: string, structure: TypeStructure): string {
|
|
59
|
+
if (!withParam) {
|
|
60
|
+
return defaultInstantiation;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 检查是否包含泛型类型
|
|
64
|
+
if (this.containsGenericType(structure)) {
|
|
65
|
+
return `new WithEntrySerializer(${withParam}, [${this.generateTypeKeySerializers(structure)}])`;
|
|
66
|
+
} else {
|
|
67
|
+
return `new WithEntryContextualSerializer(${withParam}, [${this.generateTypeKeys(structure)}])`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 检查类型结构中是否包含泛型类型参数
|
|
73
|
+
*/
|
|
74
|
+
protected containsGenericType(structure: TypeStructure): boolean {
|
|
75
|
+
// 检查当前节点是否为泛型
|
|
76
|
+
if (structure.kind === PropertyKind.GENERIC) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 递归检查泛型参数(如 Array<T>, Map<K,V>)
|
|
81
|
+
if (structure.args && structure.args.length > 0) {
|
|
82
|
+
return structure.args.some(arg => this.containsGenericType(arg));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 递归检查联合类型的成员(如 T | string | number)
|
|
86
|
+
if (structure.unionDetails && structure.unionDetails.length > 0) {
|
|
87
|
+
return structure.unionDetails.some(member => this.containsGenericType(member));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 生成类型键序列化器数组(用于泛型类型的with参数)
|
|
95
|
+
* @example Array<T> -> [Serializer<T>]
|
|
96
|
+
*/
|
|
97
|
+
protected generateTypeKeySerializers(structure: TypeStructure): string {
|
|
98
|
+
if (structure.args && structure.args.length > 0) {
|
|
99
|
+
// 需要导入TypeHandlerRegistry来生成实例化代码
|
|
100
|
+
const { TypeHandlerRegistry } = require('./TypeHandlerRegistry');
|
|
101
|
+
return structure.args.map((arg) => {
|
|
102
|
+
return TypeHandlerRegistry.getInstance().generateInstantiation(arg);
|
|
103
|
+
}).join(',\n');
|
|
104
|
+
}
|
|
105
|
+
return '';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 生成类型键数组(用于非泛型类型的with参数)
|
|
110
|
+
* @example Array<string> -> ['string']
|
|
111
|
+
*/
|
|
112
|
+
protected generateTypeKeys(structure: TypeStructure): string {
|
|
113
|
+
if (structure.args && structure.args.length > 0) {
|
|
114
|
+
const { TypeHandlerRegistry } = require('./TypeHandlerRegistry');
|
|
115
|
+
return structure.args.map(arg => {
|
|
116
|
+
return TypeHandlerRegistry.getInstance().generateTypeKey(arg);
|
|
117
|
+
}).join(', ');
|
|
118
|
+
}
|
|
119
|
+
return '';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { TypeNode, SyntaxKind, TypeReferenceNode } from 'ts-morph';
|
|
17
|
+
import { BaseTypeHandler } from './BaseTypeHandler';
|
|
18
|
+
import { ITypeHandler, TypeContext, ConversionDirection } from './ITypeHandler';
|
|
19
|
+
import {
|
|
20
|
+
PropertyKind,
|
|
21
|
+
TypeStructure,
|
|
22
|
+
SerializerMetadata,
|
|
23
|
+
TypeDependency,
|
|
24
|
+
ClassDetails,
|
|
25
|
+
TypeKind,
|
|
26
|
+
TypeAliasDetails,
|
|
27
|
+
InterfaceDetails
|
|
28
|
+
} from '../Types';
|
|
29
|
+
import { CustomTypeAnalyzer } from '../analyzers/CustomTypeAnalyzer';
|
|
30
|
+
import { ClassAnalyzer } from '../analyzers/ClassAnalyzer';
|
|
31
|
+
import { CustomError, ErrorCodes } from '../utils/CustomError';
|
|
32
|
+
|
|
33
|
+
export class CustomClassHandler extends BaseTypeHandler implements ITypeHandler {
|
|
34
|
+
readonly name = 'CustomClassHandler';
|
|
35
|
+
readonly kind = PropertyKind.OBJECT;
|
|
36
|
+
readonly priority = 10; // 最低优先级,作为fallback
|
|
37
|
+
|
|
38
|
+
// ==================== 阶段1: 类型识别 ====================
|
|
39
|
+
|
|
40
|
+
matches(typeNode: TypeNode): boolean {
|
|
41
|
+
// 如果是TypeReference且没有被其他Handler处理,就是自定义类
|
|
42
|
+
return typeNode.getKind() === SyntaxKind.TypeReference;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ==================== 阶段2: 类型解析 ====================
|
|
46
|
+
|
|
47
|
+
parse(typeNode: TypeNode, context: TypeContext): TypeStructure {
|
|
48
|
+
const typeRef = typeNode as TypeReferenceNode;
|
|
49
|
+
const customTypeName = typeRef.getTypeName().getText();
|
|
50
|
+
const typeArgs = typeRef.getTypeArguments();
|
|
51
|
+
|
|
52
|
+
// ✅ 递归解析泛型参数
|
|
53
|
+
const { dependencies, parsedArgs } = this.parseGenericArguments(typeArgs, context);
|
|
54
|
+
|
|
55
|
+
// ✅ 查找类型定义并收集依赖
|
|
56
|
+
const typeDependency = CustomTypeAnalyzer.getInstance()
|
|
57
|
+
.findDefinition(customTypeName, typeRef.getSourceFile());
|
|
58
|
+
|
|
59
|
+
if (!typeDependency) {
|
|
60
|
+
return this.handleMissingTypeDependency(customTypeName);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 处理 Type 类型别名:直接使用已解析的类型结构
|
|
64
|
+
if (typeDependency.typeKind === TypeKind.TYPE) {
|
|
65
|
+
return this.handleTypeAlias(typeDependency, typeNode);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 计算深度
|
|
69
|
+
const maxArgDepth = parsedArgs.length > 0
|
|
70
|
+
? Math.max(...parsedArgs.map(arg => arg.depth))
|
|
71
|
+
: 0;
|
|
72
|
+
|
|
73
|
+
if (typeDependency.typeKind === TypeKind.INTERFACE) {
|
|
74
|
+
return {
|
|
75
|
+
kind: this.kind,
|
|
76
|
+
depth: parsedArgs.length > 0 ? 1 + maxArgDepth : 0,
|
|
77
|
+
sourceText: typeNode.getText(), // ✅ 使用完整类型文本(包含泛型参数)
|
|
78
|
+
interfaceDetails: typeDependency.details as InterfaceDetails,
|
|
79
|
+
args: parsedArgs, // ✅ 填充泛型参数
|
|
80
|
+
dependencies: [typeDependency, ...dependencies] // ✅ 填充依赖
|
|
81
|
+
};
|
|
82
|
+
} else if (typeDependency.typeKind === TypeKind.CLASS) {
|
|
83
|
+
return {
|
|
84
|
+
kind: this.kind,
|
|
85
|
+
depth: parsedArgs.length > 0 ? 1 + maxArgDepth : 0,
|
|
86
|
+
sourceText: typeNode.getText(), // ✅ 使用完整类型文本(包含泛型参数)
|
|
87
|
+
classDetails: typeDependency.details as ClassDetails,
|
|
88
|
+
args: parsedArgs, // ✅ 填充泛型参数
|
|
89
|
+
dependencies: [typeDependency, ...dependencies] // ✅ 填充依赖
|
|
90
|
+
};
|
|
91
|
+
} else {
|
|
92
|
+
return {
|
|
93
|
+
kind: PropertyKind.UNKNOWN,
|
|
94
|
+
depth: 0,
|
|
95
|
+
sourceText: customTypeName,
|
|
96
|
+
args: [],
|
|
97
|
+
dependencies: []
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private parseGenericArguments(typeArgs: TypeNode[], context: TypeContext): {
|
|
103
|
+
dependencies: TypeDependency[];
|
|
104
|
+
parsedArgs: TypeStructure[]
|
|
105
|
+
} {
|
|
106
|
+
const dependencies: TypeDependency[] = [];
|
|
107
|
+
const parsedArgs: TypeStructure[] = typeArgs.map(arg => {
|
|
108
|
+
const { TypeHandlerRegistry } = require('./TypeHandlerRegistry');
|
|
109
|
+
const argStructure = TypeHandlerRegistry.getInstance().parseType(arg, {
|
|
110
|
+
...context,
|
|
111
|
+
depth: context.depth + 1
|
|
112
|
+
});
|
|
113
|
+
dependencies.push(...argStructure.dependencies);
|
|
114
|
+
return argStructure;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
return { dependencies, parsedArgs };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private handleMissingTypeDependency(customTypeName: string): TypeStructure {
|
|
121
|
+
// 未找到类型定义,返回 UNKNOWN 类型结构
|
|
122
|
+
// ClassAnalyzer 会检查是否有 @Transient 注解来决定如何处理
|
|
123
|
+
return {
|
|
124
|
+
kind: PropertyKind.UNKNOWN,
|
|
125
|
+
depth: 0,
|
|
126
|
+
sourceText: customTypeName,
|
|
127
|
+
args: [],
|
|
128
|
+
dependencies: []
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private handleTypeAlias(typeDependency: TypeDependency, typeNode: TypeNode): TypeStructure {
|
|
133
|
+
const typeAliasDetails = typeDependency.details as TypeAliasDetails;
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
...typeAliasDetails.resolvedType,
|
|
137
|
+
sourceText: typeNode.getText(), // 保留类型别名名称用于生成代码
|
|
138
|
+
dependencies: [typeDependency, ...typeAliasDetails.resolvedType.dependencies]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ==================== 阶段3: 序列化器配置 ====================
|
|
143
|
+
|
|
144
|
+
getSerializerMetadata(structure: TypeStructure, withParam?: string): SerializerMetadata {
|
|
145
|
+
const serializerName = `${structure.sourceText}Serializer`;
|
|
146
|
+
const defaultInstantiation = this.generateInstantiation(structure);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
name: serializerName,
|
|
150
|
+
typeDeclaration: this.generateTypeDeclaration(structure, withParam),
|
|
151
|
+
instantiationTemplate: this.applyWithParam(withParam, defaultInstantiation, structure),
|
|
152
|
+
encodeMethod: 'encodeSerializableElement',
|
|
153
|
+
decodeMethod: 'deserialize',
|
|
154
|
+
decoderType: `${structure.sourceText} | undefined`,
|
|
155
|
+
needsTypeAssertion: false
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ==================== 阶段4: 代码生成 ====================
|
|
160
|
+
|
|
161
|
+
generateInstantiation(structure: TypeStructure): string {
|
|
162
|
+
// ✅ 泛型对象:new UserSerializerFactory().getSerializer(T1Serializer, T2Serializer)
|
|
163
|
+
if (structure.args && structure.args.length > 0) {
|
|
164
|
+
const typeName = structure.sourceText.slice(0, structure.sourceText.indexOf('<'));
|
|
165
|
+
const argsSerializers = structure.args.map(arg => {
|
|
166
|
+
const { TypeHandlerRegistry } = require('./TypeHandlerRegistry');
|
|
167
|
+
const handler = TypeHandlerRegistry.getInstance().getHandlerForKind(arg.kind);
|
|
168
|
+
return handler.generateInstantiation(arg);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return `new ${typeName}SerializerFactory().getSerializer(${argsSerializers.join(', ')})`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ✅ 非泛型对象:UserSerializer.INSTANCE
|
|
175
|
+
const typeName = structure.sourceText;
|
|
176
|
+
return `${typeName}Serializer.INSTANCE`;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
generateTypeDeclaration(structure: TypeStructure, withParam?: string): string {
|
|
180
|
+
// ✅ 泛型对象:Serializer<User<T>>
|
|
181
|
+
if (structure.args && structure.args.length > 0) {
|
|
182
|
+
return `Serializer<${structure.sourceText}>`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (withParam) {
|
|
186
|
+
// ✅ 泛型对象:Serializer<User<T>>
|
|
187
|
+
return `Serializer<${structure.sourceText}>`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// ✅ 非泛型对象:UserSerializer
|
|
191
|
+
return `${structure.sourceText}Serializer`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
generateTypeKey(structure: TypeStructure): string {
|
|
195
|
+
// ✅ 泛型对象:{ classKey: 'User', genericTypes: [T1Key, T2Key] }
|
|
196
|
+
if (structure.args && structure.args.length > 0) {
|
|
197
|
+
const classKey = structure.sourceText.slice(0, structure.sourceText.indexOf('<'));
|
|
198
|
+
const genericKeys = structure.args.map(arg => {
|
|
199
|
+
const { TypeHandlerRegistry } = require('./TypeHandlerRegistry');
|
|
200
|
+
const handler = TypeHandlerRegistry.getInstance().getHandlerForKind(arg.kind);
|
|
201
|
+
return handler.generateTypeKey(arg);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
return `{ classKey: '${classKey}', genericTypes: [${genericKeys.join(', ')}] }`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ✅ 非泛型对象:直接返回类名
|
|
208
|
+
return structure.sourceText;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ==================== Sendable转换 ====================
|
|
212
|
+
|
|
213
|
+
generateSendableTypeDeclaration(structure: TypeStructure): string {
|
|
214
|
+
// 检查类是否启用了generateSendable
|
|
215
|
+
const classDetails = structure.classDetails;
|
|
216
|
+
if (!classDetails || !classDetails.classDecl) {
|
|
217
|
+
// 没有类声明信息,保持原类型
|
|
218
|
+
return structure.sourceText;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const classAnalysis = ClassAnalyzer.analyzeClass(classDetails.classDecl);
|
|
222
|
+
if (classAnalysis.decorators.serializable?.generateSendable) {
|
|
223
|
+
// 如果启用了generateSendable,添加Sendable前缀
|
|
224
|
+
const typeName = structure.sourceText;
|
|
225
|
+
if (structure.args && structure.args.length > 0) {
|
|
226
|
+
// 泛型类:User<T> -> SendableUser<T>
|
|
227
|
+
const className = typeName.slice(0, typeName.indexOf('<'));
|
|
228
|
+
const genericPart = typeName.slice(typeName.indexOf('<'));
|
|
229
|
+
return `Sendable${className}${genericPart}`;
|
|
230
|
+
} else {
|
|
231
|
+
// 非泛型类:User -> SendableUser
|
|
232
|
+
return `Sendable${typeName}`;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 类装饰器上面没有@Sendable or @Serializable({ generateSendable: true })装饰器
|
|
237
|
+
if (classDetails.classDecl.getDecorator('Sendable')) {
|
|
238
|
+
return structure.sourceText;
|
|
239
|
+
} else {
|
|
240
|
+
throw new CustomError(`Type ${classDetails.classDecl.getName()} is not sendable.
|
|
241
|
+
please add @Sendable or @Serializable({ generateSendable: true })`, ErrorCodes.NOT_SENDABLE);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 未启用generateSendable,返回原类型
|
|
245
|
+
return structure.sourceText;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
generatePropertyConversion(structure: TypeStructure, sourceValue: string, direction: ConversionDirection): string {
|
|
249
|
+
// 检查类是否启用了generateSendable
|
|
250
|
+
const classDetails = structure.classDetails;
|
|
251
|
+
if (!classDetails || !classDetails.classDecl) {
|
|
252
|
+
// 没有类声明信息,直接返回
|
|
253
|
+
return sourceValue;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const classAnalysis = ClassAnalyzer.analyzeClass(classDetails.classDecl);
|
|
257
|
+
if (classAnalysis.decorators.serializable?.generateSendable) {
|
|
258
|
+
// 启用了generateSendable,需要调用转换方法
|
|
259
|
+
if (direction === ConversionDirection.TO_SENDABLE) {
|
|
260
|
+
// 普通对象 -> Sendable对象:调用toSendable方法
|
|
261
|
+
return `${sourceValue}.toSendable()`;
|
|
262
|
+
} else {
|
|
263
|
+
// Sendable对象 -> 普通对象:调用toOrigin方法
|
|
264
|
+
return `${sourceValue}.toOrigin()`;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// 未启用generateSendable,直接传递
|
|
269
|
+
return sourceValue;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* 工厂函数
|
|
275
|
+
*/
|
|
276
|
+
export function createCustomClassHandler(): ITypeHandler {
|
|
277
|
+
return new CustomClassHandler();
|
|
278
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { SyntaxKind, TypeNode, TypeReferenceNode } from 'ts-morph';
|
|
17
|
+
import { PropertyKind, TypeStructure, SerializerMetadata } from '../Types';
|
|
18
|
+
import { BaseTypeHandler } from './BaseTypeHandler';
|
|
19
|
+
import { TypeContext, ConversionDirection, ITypeHandler } from './ITypeHandler';
|
|
20
|
+
|
|
21
|
+
class DateHandler extends BaseTypeHandler implements ITypeHandler {
|
|
22
|
+
readonly name: string = 'DateHandler';
|
|
23
|
+
kind: PropertyKind = PropertyKind.Date;
|
|
24
|
+
priority: number = 95;
|
|
25
|
+
matches(typeNode: TypeNode, context: TypeContext): boolean {
|
|
26
|
+
if (typeNode.getKind() === SyntaxKind.TypeReference) {
|
|
27
|
+
const typeRef = typeNode as TypeReferenceNode;
|
|
28
|
+
const refName = typeRef.getTypeName().getText();
|
|
29
|
+
return refName === 'Date';
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
parse(typeNode: TypeNode, context: TypeContext): TypeStructure {
|
|
34
|
+
return {
|
|
35
|
+
kind: PropertyKind.Date,
|
|
36
|
+
depth: 0,
|
|
37
|
+
sourceText: typeNode.getText(),
|
|
38
|
+
args: [],
|
|
39
|
+
dependencies: []
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
getSerializerMetadata(structure: TypeStructure, withParam?: string): SerializerMetadata {
|
|
43
|
+
return {
|
|
44
|
+
name: 'DateSerializer',
|
|
45
|
+
typeDeclaration: 'DateSerializer',
|
|
46
|
+
instantiationTemplate: 'DateSerializer.INSTANCE',
|
|
47
|
+
encodeMethod: 'encodeSerializableElement',
|
|
48
|
+
decodeMethod: 'deserialize',
|
|
49
|
+
decoderType: 'Date | undefined',
|
|
50
|
+
needsTypeAssertion: false
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
generateInstantiation(structure: TypeStructure): string {
|
|
54
|
+
return 'DateSerializer.INSTANCE';
|
|
55
|
+
}
|
|
56
|
+
generateTypeDeclaration(structure: TypeStructure): string {
|
|
57
|
+
return 'DateSerializer';
|
|
58
|
+
}
|
|
59
|
+
generateTypeKey(structure: TypeStructure): string {
|
|
60
|
+
return structure.sourceText;
|
|
61
|
+
}
|
|
62
|
+
generateSendableTypeDeclaration(structure: TypeStructure): string {
|
|
63
|
+
return structure.sourceText;
|
|
64
|
+
}
|
|
65
|
+
generatePropertyConversion(structure: TypeStructure, sourceValue: string, direction: ConversionDirection): string {
|
|
66
|
+
return sourceValue;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 工厂函数
|
|
72
|
+
*/
|
|
73
|
+
export function createDateHandler(): ITypeHandler {
|
|
74
|
+
return new DateHandler();
|
|
75
|
+
}
|