@airpower/transformer 0.1.4 → 0.1.5
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/decorator/Alias.d.ts +5 -5
- package/dist/decorator/IgnorePrefix.d.ts +5 -5
- package/dist/decorator/Prefix.d.ts +3 -4
- package/dist/decorator/ToClass.d.ts +4 -4
- package/dist/decorator/ToJson.d.ts +5 -5
- package/dist/decorator/Type.d.ts +8 -8
- package/dist/main.js +87 -102
- package/dist/transformer/DecoratorUtil.d.ts +19 -18
- package/dist/transformer/Transformer.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { TransformerField } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* ### 为属性配置别名
|
|
5
5
|
* @param alias 别名
|
|
6
6
|
*/
|
|
7
|
-
export declare function Alias(alias: string): (
|
|
7
|
+
export declare function Alias<T extends Transformer>(alias: string): (instance: T, field: keyof T) => void;
|
|
8
8
|
/**
|
|
9
9
|
* ### 获取属性的别名
|
|
10
10
|
* @returns 别名
|
|
11
|
-
* @param
|
|
12
|
-
* @param
|
|
11
|
+
* @param Class 目标类
|
|
12
|
+
* @param field 属性名
|
|
13
13
|
*/
|
|
14
|
-
export declare function getAlias<T extends Transformer>(
|
|
14
|
+
export declare function getAlias<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>): string;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { TransformerField } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* ### 属性忽略前缀
|
|
5
5
|
*/
|
|
6
|
-
export declare function IgnorePrefix(): (
|
|
6
|
+
export declare function IgnorePrefix<T extends Transformer>(): (instance: T, field: keyof T) => void;
|
|
7
7
|
/**
|
|
8
8
|
* ### 获取属性是否忽略前缀
|
|
9
9
|
* @returns 布尔
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
10
|
+
* @param Class 目标类
|
|
11
|
+
* @param field 属性名
|
|
12
12
|
*/
|
|
13
|
-
export declare function getIgnorePrefix<T extends Transformer>(
|
|
13
|
+
export declare function getIgnorePrefix<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>): boolean;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import { DecoratorTarget } from '../type';
|
|
3
2
|
/**
|
|
4
3
|
* ### 标记类转换的前缀
|
|
5
4
|
* @param prefix 转换前缀
|
|
6
5
|
*/
|
|
7
|
-
export declare function Prefix(prefix: string): (
|
|
6
|
+
export declare function Prefix<T extends Transformer>(prefix: string): (Class: ITransformerConstructor<T>) => void;
|
|
8
7
|
/**
|
|
9
8
|
* ### 获取类转换的前缀
|
|
10
|
-
* @param
|
|
9
|
+
* @param Class 目标类
|
|
11
10
|
*/
|
|
12
|
-
export declare function getPrefix<T extends Transformer>(
|
|
11
|
+
export declare function getPrefix<T extends Transformer>(Class: ITransformerConstructor<T>): string;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { IJson, ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { TransformerField } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* ### 自定义转换到 `Class` 的方法
|
|
5
5
|
* @param func 方法
|
|
6
6
|
*/
|
|
7
|
-
export declare function ToClass(func: (json: IJson) => unknown): (
|
|
7
|
+
export declare function ToClass<T extends Transformer>(func: (json: IJson) => unknown): (instance: T, field: keyof T) => void;
|
|
8
8
|
/**
|
|
9
9
|
* ### 获取自定义转换到 `Class` 的方法
|
|
10
10
|
* @param target 目标类
|
|
11
|
-
* @param
|
|
11
|
+
* @param field 属性名
|
|
12
12
|
*/
|
|
13
|
-
export declare function getToClass<T extends Transformer>(target: ITransformerConstructor<T
|
|
13
|
+
export declare function getToClass<T extends Transformer>(target: ITransformerConstructor<T>, field: TransformerField<T>): (json: IJson) => unknown;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { TransformerField } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* ### 自定义转换到 `JSON` 的方法
|
|
5
5
|
* @param func 方法
|
|
6
6
|
*/
|
|
7
|
-
export declare function ToJson<
|
|
7
|
+
export declare function ToJson<T extends Transformer>(func: (instance: T) => unknown): (instance: T, key: keyof T) => void;
|
|
8
8
|
/**
|
|
9
9
|
* ### 获取自定义转换到 `JSON` 的方法
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
10
|
+
* @param Class 目标类
|
|
11
|
+
* @param field 属性名
|
|
12
12
|
*/
|
|
13
|
-
export declare function getToJson<T extends Transformer>(
|
|
13
|
+
export declare function getToJson<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>): (instance: T) => unknown;
|
package/dist/decorator/Type.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
2
|
-
import {
|
|
2
|
+
import { TransformerField } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* ### 为属性配置强制类型
|
|
5
5
|
* @param type 强制转换的类型
|
|
6
6
|
* @param array 是否固定数组
|
|
7
7
|
*/
|
|
8
|
-
export declare function Type<T extends Transformer>(type: ITransformerConstructor<
|
|
8
|
+
export declare function Type<T extends Transformer, TO extends Transformer>(type: ITransformerConstructor<TO>, array?: boolean): (instance: T, field: keyof T) => void;
|
|
9
9
|
/**
|
|
10
10
|
* ### 获取属性的强制类型
|
|
11
11
|
* @returns 别名
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
12
|
+
* @param Class 目标类
|
|
13
|
+
* @param field 属性名
|
|
14
14
|
*/
|
|
15
|
-
export declare function getType<T extends Transformer>(
|
|
15
|
+
export declare function getType<T extends Transformer, TO extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>): ITransformerConstructor<TO> | undefined;
|
|
16
16
|
/**
|
|
17
17
|
* ### 获取属性是否强制数组
|
|
18
18
|
* @returns 别名
|
|
19
|
-
* @param
|
|
20
|
-
* @param
|
|
19
|
+
* @param Class 目标类
|
|
20
|
+
* @param field 属性名
|
|
21
21
|
*/
|
|
22
|
-
export declare function getArray<T extends Transformer>(
|
|
22
|
+
export declare function getArray<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>): boolean;
|
package/dist/main.js
CHANGED
|
@@ -4,120 +4,107 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
4
4
|
class DecoratorUtil {
|
|
5
5
|
/**
|
|
6
6
|
* ### 设置一个类配置项
|
|
7
|
-
* @param
|
|
7
|
+
* @param Class 目标类
|
|
8
8
|
* @param classConfigKey 配置项索引键值
|
|
9
9
|
* @param classConfig 配置的参数
|
|
10
10
|
*/
|
|
11
|
-
static setClassConfig(
|
|
12
|
-
this.setProperty(
|
|
11
|
+
static setClassConfig(Class, classConfigKey, classConfig) {
|
|
12
|
+
this.setProperty(Class.prototype, classConfigKey, classConfig);
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
15
|
* ### 递归获取指定类的配置项
|
|
16
|
-
* @param
|
|
16
|
+
* @param Class 目标类
|
|
17
17
|
* @param classConfigKey 配置项的Key
|
|
18
18
|
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
19
19
|
* @param isObject `可选` 是否是对象配置
|
|
20
20
|
*/
|
|
21
|
-
static getClassConfig(
|
|
22
|
-
|
|
23
|
-
target = target.prototype;
|
|
24
|
-
}
|
|
25
|
-
let classConfig = Reflect.get(target, classConfigKey);
|
|
21
|
+
static getClassConfig(Class, classConfigKey, defaultValue = void 0, isObject = false) {
|
|
22
|
+
let classConfig = Reflect.get(Class, classConfigKey);
|
|
26
23
|
if (!isObject) {
|
|
27
24
|
if (classConfig) {
|
|
28
25
|
return classConfig;
|
|
29
26
|
}
|
|
30
|
-
const
|
|
31
|
-
if (!
|
|
27
|
+
const SuperClass2 = Reflect.getPrototypeOf(Class);
|
|
28
|
+
if (!SuperClass2 || SuperClass2.prototype.constructor.name === Transformer.name) {
|
|
32
29
|
return void 0;
|
|
33
30
|
}
|
|
34
|
-
return this.getClassConfig(
|
|
31
|
+
return this.getClassConfig(SuperClass2, classConfigKey);
|
|
35
32
|
}
|
|
36
33
|
classConfig = classConfig || {};
|
|
37
|
-
const
|
|
38
|
-
if (!
|
|
34
|
+
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
35
|
+
if (!SuperClass || SuperClass.constructor.name === Transformer.name) {
|
|
39
36
|
return defaultValue;
|
|
40
37
|
}
|
|
41
38
|
return {
|
|
42
|
-
...this.getClassConfig(
|
|
39
|
+
...this.getClassConfig(SuperClass, classConfigKey, defaultValue, isObject),
|
|
43
40
|
...classConfig
|
|
44
41
|
};
|
|
45
42
|
}
|
|
46
43
|
/**
|
|
47
44
|
* ### 设置一个属性配置项
|
|
48
|
-
* @param
|
|
49
|
-
* @param
|
|
45
|
+
* @param instance 目标实例
|
|
46
|
+
* @param field 属性
|
|
50
47
|
* @param fieldConfigKey 配置项索引键值
|
|
51
48
|
* @param fieldConfig 配置的参数
|
|
52
49
|
* @param fieldListKey `可选` 类配置项列表索引值
|
|
53
50
|
*/
|
|
54
|
-
static setFieldConfig(
|
|
51
|
+
static setFieldConfig(instance, field, fieldConfigKey, fieldConfig, fieldListKey) {
|
|
55
52
|
if (fieldListKey) {
|
|
56
|
-
this.addFieldDecoratorKey(
|
|
53
|
+
this.addFieldDecoratorKey(instance, field, fieldListKey);
|
|
57
54
|
}
|
|
58
|
-
this.setProperty(
|
|
55
|
+
this.setProperty(instance, `${fieldConfigKey}[${field.toString()}]`, fieldConfig);
|
|
59
56
|
}
|
|
60
57
|
/**
|
|
61
58
|
* ### 获取类指定属性的指定类型的配置
|
|
62
|
-
* @param
|
|
63
|
-
* @param
|
|
59
|
+
* @param Class 目标类
|
|
60
|
+
* @param field 属性
|
|
64
61
|
* @param fieldConfigKey FieldConfigKey
|
|
65
62
|
* @param isObject `可选` 是否对象配置
|
|
66
63
|
*/
|
|
67
|
-
static getFieldConfig(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
let fieldConfig = Reflect.get(target, `${fieldConfigKey}[${key}]`);
|
|
64
|
+
static getFieldConfig(Class, field, fieldConfigKey, isObject = false) {
|
|
65
|
+
let fieldConfig = Reflect.get(Class.prototype, `${fieldConfigKey}[${field.toString()}]`);
|
|
66
|
+
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
72
67
|
if (!isObject) {
|
|
73
68
|
if (fieldConfig !== void 0) {
|
|
74
69
|
return fieldConfig;
|
|
75
70
|
}
|
|
76
|
-
|
|
77
|
-
if (!superClass2 || superClass2.constructor.name === Transformer.name) {
|
|
71
|
+
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
78
72
|
return void 0;
|
|
79
73
|
}
|
|
80
|
-
return this.getFieldConfig(
|
|
74
|
+
return this.getFieldConfig(SuperClass, field, fieldConfigKey);
|
|
81
75
|
}
|
|
82
76
|
fieldConfig = fieldConfig || {};
|
|
83
|
-
|
|
84
|
-
if (!superClass || superClass.constructor.name === Transformer.name) {
|
|
77
|
+
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
85
78
|
return {};
|
|
86
79
|
}
|
|
87
80
|
return {
|
|
88
|
-
...this.getFieldConfig(
|
|
81
|
+
...this.getFieldConfig(SuperClass, field, fieldConfigKey, true),
|
|
89
82
|
...fieldConfig
|
|
90
83
|
};
|
|
91
84
|
}
|
|
92
85
|
/**
|
|
93
86
|
* ### 获取类标记了装饰器的属性列表
|
|
94
|
-
* @param
|
|
87
|
+
* @param Class 目标类
|
|
95
88
|
* @param fieldConfigKey FieldConfigKey
|
|
96
89
|
* @param list `递归参数` 无需传入
|
|
97
90
|
*/
|
|
98
|
-
static getFieldList(
|
|
99
|
-
|
|
100
|
-
target = target.prototype;
|
|
101
|
-
}
|
|
102
|
-
const fieldList = Reflect.get(target, fieldConfigKey) || [];
|
|
91
|
+
static getFieldList(Class, fieldConfigKey, list = []) {
|
|
92
|
+
const fieldList = Reflect.get(Class.prototype, fieldConfigKey) || [];
|
|
103
93
|
fieldList.forEach((item) => list.includes(item) || list.push(item));
|
|
104
|
-
const
|
|
105
|
-
if (!
|
|
94
|
+
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
95
|
+
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
106
96
|
return list;
|
|
107
97
|
}
|
|
108
|
-
return this.getFieldList(
|
|
98
|
+
return this.getFieldList(SuperClass, fieldConfigKey, list);
|
|
109
99
|
}
|
|
110
100
|
/**
|
|
111
101
|
* ### 反射添加属性
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
102
|
+
* @param instance 目标属性
|
|
103
|
+
* @param propertyKey 配置key
|
|
114
104
|
* @param value 配置值
|
|
115
105
|
*/
|
|
116
|
-
static setProperty(
|
|
117
|
-
|
|
118
|
-
target = target.prototype;
|
|
119
|
-
}
|
|
120
|
-
Reflect.defineProperty(target, key, {
|
|
106
|
+
static setProperty(instance, propertyKey, value) {
|
|
107
|
+
Reflect.defineProperty(instance, propertyKey, {
|
|
121
108
|
enumerable: false,
|
|
122
109
|
value,
|
|
123
110
|
writable: false,
|
|
@@ -126,19 +113,14 @@ class DecoratorUtil {
|
|
|
126
113
|
}
|
|
127
114
|
/**
|
|
128
115
|
* ### 设置一个属性的包含装饰器索引
|
|
129
|
-
* @param
|
|
130
|
-
* @param
|
|
131
|
-
* @param
|
|
116
|
+
* @param instance 目标类
|
|
117
|
+
* @param field 属性
|
|
118
|
+
* @param propertyKey 类配置项列表索引值
|
|
132
119
|
*/
|
|
133
|
-
static addFieldDecoratorKey(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
console.warn(target, fieldListKey, key);
|
|
138
|
-
const list = Reflect.get(target, fieldListKey) || [];
|
|
139
|
-
list.push(key);
|
|
140
|
-
console.warn(target, fieldListKey, list);
|
|
141
|
-
this.setProperty(target, fieldListKey, list);
|
|
120
|
+
static addFieldDecoratorKey(instance, field, propertyKey) {
|
|
121
|
+
const list = Reflect.get(instance, propertyKey) || [];
|
|
122
|
+
list.push(field);
|
|
123
|
+
this.setProperty(instance, propertyKey, list);
|
|
142
124
|
}
|
|
143
125
|
}
|
|
144
126
|
/**
|
|
@@ -192,10 +174,10 @@ class Transformer {
|
|
|
192
174
|
const instance = new Class();
|
|
193
175
|
const fieldList = Object.keys(instance);
|
|
194
176
|
for (const field of fieldList) {
|
|
195
|
-
const jsonKey = this.getJsonKey(
|
|
177
|
+
const jsonKey = this.getJsonKey(Class, field);
|
|
196
178
|
const fieldData = json[jsonKey];
|
|
197
179
|
instance[field] = fieldData;
|
|
198
|
-
const toClass = getToClass(
|
|
180
|
+
const toClass = getToClass(Class, field);
|
|
199
181
|
if (toClass !== void 0) {
|
|
200
182
|
try {
|
|
201
183
|
;
|
|
@@ -205,8 +187,8 @@ class Transformer {
|
|
|
205
187
|
continue;
|
|
206
188
|
}
|
|
207
189
|
}
|
|
208
|
-
const FieldTypeClass = getType(
|
|
209
|
-
const isArray = getArray(
|
|
190
|
+
const FieldTypeClass = getType(Class, field);
|
|
191
|
+
const isArray = getArray(Class, field);
|
|
210
192
|
if (isArray) {
|
|
211
193
|
const fieldValueList = [];
|
|
212
194
|
if (typeof fieldData === "object" && Array.isArray(fieldData)) {
|
|
@@ -240,7 +222,7 @@ class Transformer {
|
|
|
240
222
|
}
|
|
241
223
|
}
|
|
242
224
|
for (const fieldKey of fieldList) {
|
|
243
|
-
const alias = getAlias(
|
|
225
|
+
const alias = getAlias(Class, fieldKey) || fieldKey;
|
|
244
226
|
if (alias === fieldKey) {
|
|
245
227
|
continue;
|
|
246
228
|
}
|
|
@@ -250,19 +232,19 @@ class Transformer {
|
|
|
250
232
|
}
|
|
251
233
|
/**
|
|
252
234
|
* ### 获取 JSON 的 key
|
|
253
|
-
* @param
|
|
235
|
+
* @param Class 目标实例
|
|
254
236
|
* @param field 属性 key
|
|
255
237
|
*/
|
|
256
|
-
static getJsonKey(
|
|
257
|
-
const alias = getAlias(
|
|
238
|
+
static getJsonKey(Class, field) {
|
|
239
|
+
const alias = getAlias(Class, field);
|
|
258
240
|
if (alias) {
|
|
259
241
|
return alias;
|
|
260
242
|
}
|
|
261
|
-
const prefix = getPrefix(
|
|
243
|
+
const prefix = getPrefix(Class);
|
|
262
244
|
if (!prefix) {
|
|
263
245
|
return field;
|
|
264
246
|
}
|
|
265
|
-
const ignorePrefix = getIgnorePrefix(
|
|
247
|
+
const ignorePrefix = getIgnorePrefix(Class, field);
|
|
266
248
|
if (!ignorePrefix) {
|
|
267
249
|
return prefix + field;
|
|
268
250
|
}
|
|
@@ -314,6 +296,7 @@ class Transformer {
|
|
|
314
296
|
* 会自动进行数据别名转换
|
|
315
297
|
*/
|
|
316
298
|
toJson() {
|
|
299
|
+
const Class = this.constructor;
|
|
317
300
|
const fieldList = Object.keys(this);
|
|
318
301
|
const json = {};
|
|
319
302
|
for (const field of fieldList) {
|
|
@@ -321,9 +304,9 @@ class Transformer {
|
|
|
321
304
|
if (data === null || data === void 0) {
|
|
322
305
|
continue;
|
|
323
306
|
}
|
|
324
|
-
const jsonKey = Transformer.getJsonKey(
|
|
307
|
+
const jsonKey = Transformer.getJsonKey(Class, field);
|
|
325
308
|
json[jsonKey] = data;
|
|
326
|
-
const toJson = getToJson(
|
|
309
|
+
const toJson = getToJson(Class, field);
|
|
327
310
|
if (toJson !== void 0) {
|
|
328
311
|
try {
|
|
329
312
|
json[jsonKey] = toJson(this);
|
|
@@ -351,56 +334,58 @@ class Transformer {
|
|
|
351
334
|
return json;
|
|
352
335
|
}
|
|
353
336
|
}
|
|
354
|
-
const KEY$5 =
|
|
337
|
+
const KEY$5 = "[Alias]";
|
|
355
338
|
function Alias(alias) {
|
|
356
|
-
return (
|
|
339
|
+
return (instance, field) => {
|
|
340
|
+
DecoratorUtil.setFieldConfig(instance, field, KEY$5, alias);
|
|
341
|
+
};
|
|
357
342
|
}
|
|
358
|
-
function getAlias(
|
|
359
|
-
return (DecoratorUtil.getFieldConfig(
|
|
343
|
+
function getAlias(Class, field) {
|
|
344
|
+
return (DecoratorUtil.getFieldConfig(Class, field, KEY$5) || "").toString();
|
|
360
345
|
}
|
|
361
|
-
const KEY$4 =
|
|
346
|
+
const KEY$4 = "[IgnorePrefix]";
|
|
362
347
|
function IgnorePrefix() {
|
|
363
|
-
return (
|
|
348
|
+
return (instance, field) => DecoratorUtil.setFieldConfig(instance, field, KEY$4, true);
|
|
364
349
|
}
|
|
365
|
-
function getIgnorePrefix(
|
|
366
|
-
return !!DecoratorUtil.getFieldConfig(
|
|
350
|
+
function getIgnorePrefix(Class, field) {
|
|
351
|
+
return !!DecoratorUtil.getFieldConfig(Class, field, KEY$4);
|
|
367
352
|
}
|
|
368
|
-
const KEY$3 =
|
|
353
|
+
const KEY$3 = "[Prefix]";
|
|
369
354
|
function Prefix(prefix) {
|
|
370
|
-
return (
|
|
355
|
+
return (Class) => DecoratorUtil.setClassConfig(Class, KEY$3, prefix);
|
|
371
356
|
}
|
|
372
|
-
function getPrefix(
|
|
373
|
-
return DecoratorUtil.getClassConfig(
|
|
357
|
+
function getPrefix(Class) {
|
|
358
|
+
return DecoratorUtil.getClassConfig(Class, KEY$3, "");
|
|
374
359
|
}
|
|
375
|
-
const KEY$2 =
|
|
360
|
+
const KEY$2 = "[ToClass]";
|
|
376
361
|
function ToClass(func) {
|
|
377
|
-
return (
|
|
362
|
+
return (instance, field) => DecoratorUtil.setFieldConfig(instance, field, KEY$2, func);
|
|
378
363
|
}
|
|
379
|
-
function getToClass(target,
|
|
380
|
-
return DecoratorUtil.getFieldConfig(target,
|
|
364
|
+
function getToClass(target, field) {
|
|
365
|
+
return DecoratorUtil.getFieldConfig(target, field, KEY$2);
|
|
381
366
|
}
|
|
382
|
-
const KEY$1 =
|
|
367
|
+
const KEY$1 = "[ToJson]";
|
|
383
368
|
function ToJson(func) {
|
|
384
|
-
return (
|
|
369
|
+
return (instance, key) => DecoratorUtil.setFieldConfig(instance, key, KEY$1, func);
|
|
385
370
|
}
|
|
386
|
-
function getToJson(
|
|
387
|
-
return DecoratorUtil.getFieldConfig(
|
|
371
|
+
function getToJson(Class, field) {
|
|
372
|
+
return DecoratorUtil.getFieldConfig(Class, field, KEY$1);
|
|
388
373
|
}
|
|
389
|
-
const KEY =
|
|
390
|
-
const KEY_ARRAY =
|
|
374
|
+
const KEY = "[Type]";
|
|
375
|
+
const KEY_ARRAY = "[Array]";
|
|
391
376
|
function Type(type, array = false) {
|
|
392
|
-
return (
|
|
393
|
-
DecoratorUtil.setFieldConfig(
|
|
377
|
+
return (instance, field) => {
|
|
378
|
+
DecoratorUtil.setFieldConfig(instance, field, KEY, type);
|
|
394
379
|
if (array) {
|
|
395
|
-
DecoratorUtil.setFieldConfig(
|
|
380
|
+
DecoratorUtil.setFieldConfig(instance, field, KEY_ARRAY, array);
|
|
396
381
|
}
|
|
397
382
|
};
|
|
398
383
|
}
|
|
399
|
-
function getType(
|
|
400
|
-
return DecoratorUtil.getFieldConfig(
|
|
384
|
+
function getType(Class, field) {
|
|
385
|
+
return DecoratorUtil.getFieldConfig(Class, field, KEY) || void 0;
|
|
401
386
|
}
|
|
402
|
-
function getArray(
|
|
403
|
-
return !!DecoratorUtil.getFieldConfig(
|
|
387
|
+
function getArray(Class, field) {
|
|
388
|
+
return !!DecoratorUtil.getFieldConfig(Class, field, KEY_ARRAY);
|
|
404
389
|
}
|
|
405
390
|
export {
|
|
406
391
|
Alias,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { DecoratorData,
|
|
1
|
+
import { DecoratorData, TransformerField } from '../type';
|
|
2
|
+
import { ITransformerConstructor, Transformer } from './index';
|
|
2
3
|
/**
|
|
3
4
|
* # 装饰器工具类
|
|
4
5
|
*
|
|
@@ -11,55 +12,55 @@ export declare class DecoratorUtil {
|
|
|
11
12
|
static readonly DecoratorKeyPrefix = "[AirPower]";
|
|
12
13
|
/**
|
|
13
14
|
* ### 设置一个类配置项
|
|
14
|
-
* @param
|
|
15
|
+
* @param Class 目标类
|
|
15
16
|
* @param classConfigKey 配置项索引键值
|
|
16
17
|
* @param classConfig 配置的参数
|
|
17
18
|
*/
|
|
18
|
-
static setClassConfig(
|
|
19
|
+
static setClassConfig<T extends Transformer>(Class: ITransformerConstructor<T>, classConfigKey: string, classConfig: unknown): void;
|
|
19
20
|
/**
|
|
20
21
|
* ### 递归获取指定类的配置项
|
|
21
|
-
* @param
|
|
22
|
+
* @param Class 目标类
|
|
22
23
|
* @param classConfigKey 配置项的Key
|
|
23
24
|
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
24
25
|
* @param isObject `可选` 是否是对象配置
|
|
25
26
|
*/
|
|
26
|
-
static getClassConfig(
|
|
27
|
+
static getClassConfig<T extends Transformer>(Class: ITransformerConstructor<T>, classConfigKey: string, defaultValue?: unknown, isObject?: boolean): DecoratorData;
|
|
27
28
|
/**
|
|
28
29
|
* ### 设置一个属性配置项
|
|
29
|
-
* @param
|
|
30
|
-
* @param
|
|
30
|
+
* @param instance 目标实例
|
|
31
|
+
* @param field 属性
|
|
31
32
|
* @param fieldConfigKey 配置项索引键值
|
|
32
33
|
* @param fieldConfig 配置的参数
|
|
33
34
|
* @param fieldListKey `可选` 类配置项列表索引值
|
|
34
35
|
*/
|
|
35
|
-
static setFieldConfig(
|
|
36
|
+
static setFieldConfig<T extends Transformer>(instance: T, field: keyof T, fieldConfigKey: string, fieldConfig: unknown, fieldListKey?: string): void;
|
|
36
37
|
/**
|
|
37
38
|
* ### 获取类指定属性的指定类型的配置
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
39
|
+
* @param Class 目标类
|
|
40
|
+
* @param field 属性
|
|
40
41
|
* @param fieldConfigKey FieldConfigKey
|
|
41
42
|
* @param isObject `可选` 是否对象配置
|
|
42
43
|
*/
|
|
43
|
-
static getFieldConfig(
|
|
44
|
+
static getFieldConfig<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>, fieldConfigKey: string, isObject?: boolean): DecoratorData;
|
|
44
45
|
/**
|
|
45
46
|
* ### 获取类标记了装饰器的属性列表
|
|
46
|
-
* @param
|
|
47
|
+
* @param Class 目标类
|
|
47
48
|
* @param fieldConfigKey FieldConfigKey
|
|
48
49
|
* @param list `递归参数` 无需传入
|
|
49
50
|
*/
|
|
50
|
-
static getFieldList(
|
|
51
|
+
static getFieldList<T extends Transformer>(Class: ITransformerConstructor<T>, fieldConfigKey: string, list?: string[]): string[];
|
|
51
52
|
/**
|
|
52
53
|
* ### 反射添加属性
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
54
|
+
* @param instance 目标属性
|
|
55
|
+
* @param propertyKey 配置key
|
|
55
56
|
* @param value 配置值
|
|
56
57
|
*/
|
|
57
58
|
private static setProperty;
|
|
58
59
|
/**
|
|
59
60
|
* ### 设置一个属性的包含装饰器索引
|
|
60
|
-
* @param
|
|
61
|
-
* @param
|
|
62
|
-
* @param
|
|
61
|
+
* @param instance 目标类
|
|
62
|
+
* @param field 属性
|
|
63
|
+
* @param propertyKey 类配置项列表索引值
|
|
63
64
|
*/
|
|
64
65
|
private static addFieldDecoratorKey;
|
|
65
66
|
}
|
|
@@ -32,7 +32,7 @@ export declare class Transformer {
|
|
|
32
32
|
static parse<T extends Transformer>(json: IJson | undefined, Class: ITransformerConstructor<T>): T;
|
|
33
33
|
/**
|
|
34
34
|
* ### 获取 JSON 的 key
|
|
35
|
-
* @param
|
|
35
|
+
* @param Class 目标实例
|
|
36
36
|
* @param field 属性 key
|
|
37
37
|
*/
|
|
38
38
|
private static getJsonKey;
|