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