rads-db 3.0.19 → 3.0.21
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/index.cjs +49 -55
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +49 -55
- package/drivers/azureCosmos.cjs +15 -17
- package/drivers/azureCosmos.mjs +7 -10
- package/drivers/azureStorageBlob.cjs +19 -20
- package/drivers/azureStorageBlob.mjs +17 -20
- package/drivers/memory.cjs +30 -19
- package/drivers/memory.mjs +20 -15
- package/integrations/lib.cjs +229 -171
- package/integrations/lib.d.ts +1 -1
- package/integrations/lib.mjs +198 -130
- package/package.json +1 -1
package/integrations/lib.mjs
CHANGED
|
@@ -3,8 +3,58 @@ import _ from "lodash";
|
|
|
3
3
|
import pluralize from "pluralize";
|
|
4
4
|
const supportedPrimitiveTypes = ["string", "number", "boolean", "Record<string, string>", "Record<string, any>"];
|
|
5
5
|
export function parseSchema(typescriptFiles) {
|
|
6
|
+
const typeNodesMap = getTypeNodesMap(typescriptFiles);
|
|
7
|
+
const schema = getSchema(typeNodesMap);
|
|
8
|
+
resolveIsExtending(schema);
|
|
9
|
+
fillComputedDefinitionsForType(schema);
|
|
10
|
+
verifyDefaultValueTypes(schema, typeNodesMap);
|
|
11
|
+
verifyRelationFields(schema);
|
|
12
|
+
return schema;
|
|
13
|
+
}
|
|
14
|
+
function resolveIsExtending(schema) {
|
|
15
|
+
for (const key in schema) {
|
|
16
|
+
const isExtendingType = schema[key].isExtending;
|
|
17
|
+
if (!isExtendingType)
|
|
18
|
+
continue;
|
|
19
|
+
if (!schema[isExtendingType])
|
|
20
|
+
throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
21
|
+
schema[key].fields = { ...schema[isExtendingType].fields, ...schema[key].fields };
|
|
22
|
+
schema[key].decorators = { ...schema[isExtendingType].decorators, ...schema[key].decorators };
|
|
23
|
+
if (schema[key].decorators.entity && !schema[key].fields?.id) {
|
|
24
|
+
throw new Error(`Entity "${key}" must have an id`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function getSchema(typeNodesMap) {
|
|
6
29
|
const result = {};
|
|
7
|
-
const
|
|
30
|
+
for (const key in typeNodesMap) {
|
|
31
|
+
const { node, name } = typeNodesMap[key];
|
|
32
|
+
if (![SyntaxKind.ClassDeclaration, SyntaxKind.TypeAliasDeclaration].includes(node.kind)) {
|
|
33
|
+
throw new Error(`Unexpected type kind - "${name}"`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (const key in typeNodesMap) {
|
|
37
|
+
if (result[key])
|
|
38
|
+
continue;
|
|
39
|
+
const { node, sourceFile, name } = typeNodesMap[key];
|
|
40
|
+
if (node.kind !== SyntaxKind.ClassDeclaration)
|
|
41
|
+
continue;
|
|
42
|
+
const parsedClass = parseClassDeclaration(node, name, { typeNodesMap, sourceFile, result });
|
|
43
|
+
result[key] = parsedClass;
|
|
44
|
+
}
|
|
45
|
+
for (const key in typeNodesMap) {
|
|
46
|
+
if (result[key])
|
|
47
|
+
continue;
|
|
48
|
+
const { node, sourceFile, name } = typeNodesMap[key];
|
|
49
|
+
if (node.kind !== SyntaxKind.TypeAliasDeclaration)
|
|
50
|
+
continue;
|
|
51
|
+
const parsedClass = parseTypeAliasDeclaration(node, name, { typeNodesMap, sourceFile, result });
|
|
52
|
+
result[key] = parsedClass;
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
function getTypeNodesMap(typescriptFiles) {
|
|
57
|
+
const typeNodesMap = {};
|
|
8
58
|
for (const key in typescriptFiles) {
|
|
9
59
|
const text = typescriptFiles[key];
|
|
10
60
|
const sourceFile = createSourceFile(`${key}.ts`, text, ScriptTarget.Latest);
|
|
@@ -14,61 +64,41 @@ export function parseSchema(typescriptFiles) {
|
|
|
14
64
|
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
15
65
|
throw new Error("Cannot detect class name");
|
|
16
66
|
const name = nameNode.text;
|
|
17
|
-
|
|
67
|
+
typeNodesMap[name] = { name, node: cd, sourceFile };
|
|
18
68
|
}
|
|
19
|
-
if (!
|
|
69
|
+
if (!typeNodesMap[key]) {
|
|
20
70
|
throw new Error(`File ${key}.ts must contain class declaration with name "${key}"`);
|
|
21
71
|
}
|
|
22
72
|
}
|
|
23
|
-
|
|
24
|
-
if (result[key])
|
|
25
|
-
continue;
|
|
26
|
-
const { node, sourceFile, name } = typesMap[key];
|
|
27
|
-
const parsedClass = parseType(node, name, { typesMap, sourceFile, result });
|
|
28
|
-
if (parsedClass.name !== key)
|
|
29
|
-
throw new Error(`File name must correspond to exported class name ("${key}", "${parsedClass.name}")`);
|
|
30
|
-
result[key] = parsedClass;
|
|
31
|
-
}
|
|
32
|
-
for (const key in result) {
|
|
33
|
-
const isExtendingType = result[key].isExtending;
|
|
34
|
-
if (!isExtendingType)
|
|
35
|
-
continue;
|
|
36
|
-
if (!result[isExtendingType])
|
|
37
|
-
throw new Error(`Unknown type: "${isExtendingType}"`);
|
|
38
|
-
result[key].fields = { ...result[isExtendingType].fields, ...result[key].fields };
|
|
39
|
-
result[key].decorators = { ...result[isExtendingType].decorators, ...result[key].decorators };
|
|
40
|
-
if (result[key].decorators.entity && !result[key].fields?.id) {
|
|
41
|
-
throw new Error(`Entity "${key}" must have an id`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
fillComputedDefinitionsForType(result);
|
|
45
|
-
processThisReferenceDefaultValues(result, typesMap);
|
|
46
|
-
verifyRelationFields(result);
|
|
47
|
-
return result;
|
|
73
|
+
return typeNodesMap;
|
|
48
74
|
}
|
|
49
|
-
function
|
|
50
|
-
for (const key in
|
|
51
|
-
const type =
|
|
75
|
+
function verifyDefaultValueTypes(schema, typeNodesMap) {
|
|
76
|
+
for (const key in schema) {
|
|
77
|
+
const type = schema[key];
|
|
52
78
|
const fields = type.fields;
|
|
53
79
|
if (!fields)
|
|
54
80
|
continue;
|
|
55
81
|
for (const fName in fields) {
|
|
56
82
|
const field = fields[fName];
|
|
57
|
-
if (field.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
83
|
+
if (field.defaultValue) {
|
|
84
|
+
verifyDefaultValueType(field, {
|
|
85
|
+
typeNodesMap,
|
|
86
|
+
result: schema,
|
|
87
|
+
sourceFile: typeNodesMap[key]?.sourceFile
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
for (const fName2 in fields) {
|
|
91
|
+
const field2 = fields[fName2];
|
|
92
|
+
if (field2.defaultValueCopyFrom) {
|
|
93
|
+
const sourceField = fields[field2.defaultValueCopyFrom];
|
|
94
|
+
if (!sourceField)
|
|
95
|
+
throw new Error(`Cannot find field ${key}.${field2.defaultValueCopyFrom}"`);
|
|
96
|
+
verifyDefaultValueTypeCopyFrom(field2, sourceField, {
|
|
97
|
+
typeNodesMap,
|
|
98
|
+
result: schema,
|
|
99
|
+
sourceFile: typeNodesMap[key]?.sourceFile
|
|
100
|
+
});
|
|
64
101
|
}
|
|
65
|
-
verifyDefaultValueType(
|
|
66
|
-
field.isArray || false,
|
|
67
|
-
{ type: sourceField.type, value: sourceField.defaultValue },
|
|
68
|
-
field.type,
|
|
69
|
-
supportedPrimitiveTypes,
|
|
70
|
-
{ typesMap, result, sourceFile: typesMap[key]?.sourceFile }
|
|
71
|
-
);
|
|
72
102
|
}
|
|
73
103
|
}
|
|
74
104
|
}
|
|
@@ -98,7 +128,14 @@ function getOrder(f) {
|
|
|
98
128
|
};
|
|
99
129
|
return decorator.order ?? defaultOrders[decorator.preset] ?? 0;
|
|
100
130
|
}
|
|
101
|
-
function
|
|
131
|
+
function parseClassOrTypeDeclaration(typeDeclaration, typeName, ctx) {
|
|
132
|
+
if (typeDeclaration.kind === SyntaxKind.ClassDeclaration)
|
|
133
|
+
return parseClassDeclaration(typeDeclaration, typeName, ctx);
|
|
134
|
+
if (typeDeclaration.kind === SyntaxKind.TypeAliasDeclaration)
|
|
135
|
+
return parseTypeAliasDeclaration(typeDeclaration, typeName, ctx);
|
|
136
|
+
throw new Error(`Unexpected type definition - ${typeName}`);
|
|
137
|
+
}
|
|
138
|
+
function parseClassDeclaration(typeDeclaration, typeName, ctx) {
|
|
102
139
|
if (ctx.result[typeName])
|
|
103
140
|
return ctx.result[typeName];
|
|
104
141
|
const { modifiers } = typeDeclaration;
|
|
@@ -108,49 +145,53 @@ function parseType(typeDeclaration, typeName, ctx) {
|
|
|
108
145
|
const name = nameNode.text;
|
|
109
146
|
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
110
147
|
const decorators = parseDecorators(modifiers, ctx);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
119
|
-
}
|
|
148
|
+
const classDeclaration = typeDeclaration;
|
|
149
|
+
const { members, heritageClauses } = classDeclaration;
|
|
150
|
+
const isExtendingExpr = heritageClauses?.[0]?.types?.[0]?.expression;
|
|
151
|
+
const isExtending = isExtendingExpr?.text;
|
|
152
|
+
for (const m of members) {
|
|
153
|
+
if (m.kind !== SyntaxKind.PropertyDeclaration) {
|
|
154
|
+
throw new Error(`Unexpected class member - only properties are allowed("${m.getText(ctx.sourceFile)}")`);
|
|
120
155
|
}
|
|
121
|
-
const fieldsArray = members.map((m) => parseClassMember(m, name, ctx));
|
|
122
|
-
const fields = {};
|
|
123
|
-
for (const f of fieldsArray) {
|
|
124
|
-
fields[f.name] = f;
|
|
125
|
-
}
|
|
126
|
-
const handle = _.lowerFirst(name);
|
|
127
|
-
const handlePlural = pluralize(handle);
|
|
128
|
-
const result = {
|
|
129
|
-
name,
|
|
130
|
-
handle,
|
|
131
|
-
handlePlural,
|
|
132
|
-
decorators,
|
|
133
|
-
fields,
|
|
134
|
-
isExtending,
|
|
135
|
-
comment
|
|
136
|
-
};
|
|
137
|
-
return result;
|
|
138
156
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
const typeAliasValue = typeAliasDeclaration.type;
|
|
144
|
-
const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx);
|
|
145
|
-
return { name, decorators, enumValues, comment };
|
|
146
|
-
}
|
|
147
|
-
if (typeAliasType.kind === SyntaxKind.TypeOperator && typeAliasType.operator === SyntaxKind.KeyOfKeyword) {
|
|
148
|
-
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx);
|
|
149
|
-
return { name, decorators, enumValues, comment };
|
|
150
|
-
}
|
|
151
|
-
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}. Did you mean 'class'?`);
|
|
157
|
+
const fields = {};
|
|
158
|
+
for (const m of members) {
|
|
159
|
+
const field = parseClassMember(m, fields, name, ctx);
|
|
160
|
+
fields[field.name] = field;
|
|
152
161
|
}
|
|
153
|
-
|
|
162
|
+
const handle = _.lowerFirst(name);
|
|
163
|
+
const handlePlural = pluralize(handle);
|
|
164
|
+
const result = {
|
|
165
|
+
name,
|
|
166
|
+
handle,
|
|
167
|
+
handlePlural,
|
|
168
|
+
decorators,
|
|
169
|
+
fields,
|
|
170
|
+
isExtending,
|
|
171
|
+
comment
|
|
172
|
+
};
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
function parseTypeAliasDeclaration(typeDeclaration, typeName, ctx) {
|
|
176
|
+
if (ctx.result[typeName])
|
|
177
|
+
return ctx.result[typeName];
|
|
178
|
+
const nameNode = typeDeclaration.name;
|
|
179
|
+
if (!nameNode || nameNode.kind !== SyntaxKind.Identifier)
|
|
180
|
+
throw new Error("Cannot detect class name");
|
|
181
|
+
const name = nameNode.text;
|
|
182
|
+
const comment = typeDeclaration.jsDoc?.[0]?.comment;
|
|
183
|
+
const typeAliasDeclaration = typeDeclaration;
|
|
184
|
+
const typeAliasType = typeAliasDeclaration.type;
|
|
185
|
+
if (typeAliasType.kind === SyntaxKind.UnionType) {
|
|
186
|
+
const typeAliasValue = typeAliasDeclaration.type;
|
|
187
|
+
const enumValues = getEnumValues(typeAliasValue, typeDeclaration, ctx);
|
|
188
|
+
return { name, enumValues, comment, decorators: {} };
|
|
189
|
+
}
|
|
190
|
+
if (typeAliasType.kind === SyntaxKind.TypeOperator && typeAliasType.operator === SyntaxKind.KeyOfKeyword) {
|
|
191
|
+
const enumValues = getEnumValuesFromKeyOf(typeAliasType, ctx);
|
|
192
|
+
return { name, enumValues, comment, decorators: {} };
|
|
193
|
+
}
|
|
194
|
+
throw new Error(`Unexpected type definition - ${typeDeclaration.getText(ctx.sourceFile)}. Did you mean 'class'?`);
|
|
154
195
|
}
|
|
155
196
|
function parseDecorators(modifiers, ctx) {
|
|
156
197
|
if (!modifiers)
|
|
@@ -175,27 +216,25 @@ function getEnumValues(node, parentNode, ctx) {
|
|
|
175
216
|
});
|
|
176
217
|
return _.keyBy(enumValuesArray, "name");
|
|
177
218
|
}
|
|
178
|
-
function parseClassMember(node, parentName, ctx) {
|
|
219
|
+
function parseClassMember(node, parentFields, parentName, ctx) {
|
|
179
220
|
const name = node.name.getText(ctx.sourceFile);
|
|
180
|
-
const
|
|
181
|
-
let defaultValue;
|
|
182
|
-
let defaultValueCopyFrom;
|
|
183
|
-
if (defaultValueDescription) {
|
|
184
|
-
if (defaultValueDescription.type === "thisReference") {
|
|
185
|
-
defaultValueCopyFrom = defaultValueDescription.value;
|
|
186
|
-
} else {
|
|
187
|
-
defaultValue = defaultValueDescription.value;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
221
|
+
const { defaultValue, defaultValueCopyFrom, defaultValueClass } = parseDefaultValueExpression(node.initializer, ctx);
|
|
190
222
|
const isRequired = !node.questionToken;
|
|
191
223
|
const comment = node.jsDoc?.[0]?.comment;
|
|
192
224
|
const decorators = parseDecorators(node.modifiers, ctx);
|
|
225
|
+
let defaultValueType = defaultValueClass || getPrimitiveTypeFromDefaultValue(defaultValue);
|
|
226
|
+
if (defaultValueCopyFrom) {
|
|
227
|
+
const parentField = parentFields[defaultValueCopyFrom];
|
|
228
|
+
if (!parentField)
|
|
229
|
+
throw new Error(`Cannot find field ${parentName}.${defaultValueCopyFrom}"`);
|
|
230
|
+
defaultValueType = parentField.type;
|
|
231
|
+
}
|
|
193
232
|
const { isArray, isRelation, isChange, relationDenormFields, type } = parseFieldType(
|
|
194
233
|
ctx,
|
|
195
234
|
parentName,
|
|
196
235
|
name,
|
|
197
236
|
node,
|
|
198
|
-
|
|
237
|
+
defaultValueType
|
|
199
238
|
);
|
|
200
239
|
const result = {
|
|
201
240
|
type,
|
|
@@ -211,9 +250,24 @@ function parseClassMember(node, parentName, ctx) {
|
|
|
211
250
|
};
|
|
212
251
|
if (!_.isEmpty(decorators))
|
|
213
252
|
result.decorators = decorators;
|
|
253
|
+
if (defaultValueClass)
|
|
254
|
+
result.defaultValueClass = defaultValueClass;
|
|
214
255
|
return result;
|
|
215
256
|
}
|
|
216
|
-
function
|
|
257
|
+
function getPrimitiveTypeFromDefaultValue(value) {
|
|
258
|
+
if (_.isString(value))
|
|
259
|
+
return "string";
|
|
260
|
+
if (_.isNumber(value))
|
|
261
|
+
return "number";
|
|
262
|
+
if (_.isBoolean(value))
|
|
263
|
+
return "boolean";
|
|
264
|
+
if (_.isArray(value))
|
|
265
|
+
return "array";
|
|
266
|
+
if (_.isObject(value))
|
|
267
|
+
return "object";
|
|
268
|
+
return void 0;
|
|
269
|
+
}
|
|
270
|
+
function parseFieldType(ctx, parentName, fieldName, node, defaultValueType) {
|
|
217
271
|
const parsedType = {
|
|
218
272
|
isArray: false,
|
|
219
273
|
isRelation: false,
|
|
@@ -228,22 +282,19 @@ function parseFieldType(ctx, parentName, fieldName, node, defaultValueDescriptio
|
|
|
228
282
|
parseFieldTypeInlineEnum(parsedType, parentName, fieldName, ctx);
|
|
229
283
|
parseFieldTypeKeyofEnum(parsedType, parentName, fieldName, ctx);
|
|
230
284
|
parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx);
|
|
231
|
-
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ??
|
|
285
|
+
parsedType.type = parsedType.type ?? parsedType.nodeType?.getText(ctx.sourceFile) ?? defaultValueType;
|
|
232
286
|
if (!parsedType.type)
|
|
233
287
|
throw new Error(`Cannot detect property type: '${node.getText(ctx.sourceFile)}'`);
|
|
234
288
|
if (parsedType.type.startsWith("Change<")) {
|
|
235
289
|
parsedType.type = parsedType.type.slice(7, -1);
|
|
236
|
-
if (!ctx.
|
|
290
|
+
if (!ctx.typeNodesMap[parsedType.type])
|
|
237
291
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
238
292
|
parsedType.isChange = true;
|
|
239
293
|
} else {
|
|
240
|
-
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx.
|
|
294
|
+
if (!supportedPrimitiveTypes.includes(parsedType.type) && !ctx.typeNodesMap[parsedType.type] && !ctx.result[parsedType.type]) {
|
|
241
295
|
throw new Error(`Unexpected property type: '${parsedType.type}'`);
|
|
242
296
|
}
|
|
243
297
|
}
|
|
244
|
-
if (defaultValueDescription && defaultValueDescription.type !== "thisReference") {
|
|
245
|
-
verifyDefaultValueType(parsedType.isArray, defaultValueDescription, parsedType.type, supportedPrimitiveTypes, ctx);
|
|
246
|
-
}
|
|
247
298
|
return {
|
|
248
299
|
isArray: parsedType.isArray || void 0,
|
|
249
300
|
isRelation: parsedType.isRelation || void 0,
|
|
@@ -262,11 +313,11 @@ function parseFieldTypeRecordEnum(parsedType, parentName, fieldName, ctx) {
|
|
|
262
313
|
return;
|
|
263
314
|
const keyTypeName = nt.typeArguments[0].getText(ctx.sourceFile);
|
|
264
315
|
const valueTypeName = nt.typeArguments[1].getText(ctx.sourceFile);
|
|
265
|
-
const keyType = ctx.
|
|
316
|
+
const keyType = ctx.typeNodesMap[keyTypeName];
|
|
266
317
|
if (!keyType)
|
|
267
318
|
return;
|
|
268
319
|
if (!ctx.result[keyTypeName])
|
|
269
|
-
ctx.result[keyTypeName] =
|
|
320
|
+
ctx.result[keyTypeName] = parseClassOrTypeDeclaration(keyType.node, keyTypeName, ctx);
|
|
270
321
|
const enumValues = ctx.result[keyTypeName].enumValues;
|
|
271
322
|
if (!enumValues)
|
|
272
323
|
throw new Error(`Unexpected type - ${keyTypeName}`);
|
|
@@ -335,12 +386,12 @@ function getEnumValuesFromKeyOf(nodeType, ctx) {
|
|
|
335
386
|
throw new Error(`Unexpected type - ${typeReferenceNode.getText(ctx.sourceFile)}`);
|
|
336
387
|
}
|
|
337
388
|
const typeName = typeReferenceNode.typeName.text;
|
|
338
|
-
const type = ctx.
|
|
389
|
+
const type = ctx.typeNodesMap[typeName];
|
|
339
390
|
if (!type) {
|
|
340
391
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
341
392
|
}
|
|
342
393
|
if (!ctx.result[typeName])
|
|
343
|
-
ctx.result[typeName] =
|
|
394
|
+
ctx.result[typeName] = parseClassOrTypeDeclaration(type.node, typeName, ctx);
|
|
344
395
|
if (!ctx.result[typeName].fields)
|
|
345
396
|
throw new Error(`Unexpected type - ${typeName}`);
|
|
346
397
|
return _.mapValues(ctx.result[typeName].fields || {}, (v) => ({
|
|
@@ -363,24 +414,37 @@ function getRelationDenormFields(ctx, node) {
|
|
|
363
414
|
}
|
|
364
415
|
throw new Error(`Unexpected type - ${node.getText(ctx.sourceFile)}`);
|
|
365
416
|
}
|
|
366
|
-
function verifyDefaultValueType(
|
|
417
|
+
function verifyDefaultValueType(field, ctx) {
|
|
418
|
+
const { isArray, defaultValue, type } = field;
|
|
367
419
|
if (isArray) {
|
|
368
|
-
if (
|
|
369
|
-
throw new
|
|
420
|
+
if (!_.isArray(defaultValue)) {
|
|
421
|
+
throw new TypeError(`Default value type is different from field type: '${type}'`);
|
|
370
422
|
}
|
|
371
423
|
} else {
|
|
372
|
-
if (
|
|
424
|
+
if (supportedPrimitiveTypes.includes(type) && getPrimitiveTypeFromDefaultValue(defaultValue) !== type) {
|
|
373
425
|
throw new Error(`Default value type is different from field type: '${type}'`);
|
|
374
426
|
}
|
|
375
|
-
if (!ctx.result[type] && ctx.
|
|
376
|
-
ctx.result[type] =
|
|
427
|
+
if (!ctx.result[type] && ctx.typeNodesMap[type])
|
|
428
|
+
ctx.result[type] = parseClassOrTypeDeclaration(ctx.typeNodesMap[type].node, type, ctx);
|
|
377
429
|
const enumValues = ctx.result[type]?.enumValues;
|
|
378
|
-
if (enumValues && !enumValues[
|
|
430
|
+
if (enumValues && !enumValues[defaultValue]) {
|
|
379
431
|
const enumValuesStr = _.keys(enumValues).map((x) => `'x'`).join(", ");
|
|
380
432
|
throw new Error(`Default value must be one of: ${enumValuesStr}`);
|
|
381
433
|
}
|
|
382
434
|
}
|
|
383
435
|
}
|
|
436
|
+
function verifyDefaultValueTypeCopyFrom(field, sourceField, ctx) {
|
|
437
|
+
const { isArray, type } = field;
|
|
438
|
+
if (isArray) {
|
|
439
|
+
if (!sourceField.isArray) {
|
|
440
|
+
throw new TypeError(`Default value type is not an array: '${field.name}'`);
|
|
441
|
+
}
|
|
442
|
+
} else {
|
|
443
|
+
if (sourceField.type !== type) {
|
|
444
|
+
throw new Error(`Default value type is different from field type: '${field.name}'`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
384
448
|
function parseDecorator(decoratorNode, ctx) {
|
|
385
449
|
const expr = decoratorNode.expression;
|
|
386
450
|
if (expr.kind !== SyntaxKind.CallExpression)
|
|
@@ -397,28 +461,35 @@ function parseDecoratorArguments(expr, ctx) {
|
|
|
397
461
|
if (args.length > 1)
|
|
398
462
|
throw new Error(`Too many arguments - one expected: "${expr.getText(ctx.sourceFile)}"`);
|
|
399
463
|
const arg = args[0];
|
|
400
|
-
return parseLiteralNode(arg, ctx)
|
|
464
|
+
return parseLiteralNode(arg, ctx) ?? {};
|
|
401
465
|
}
|
|
402
|
-
function
|
|
466
|
+
function parseDefaultValueExpression(expr, ctx) {
|
|
403
467
|
if (!expr)
|
|
404
|
-
return
|
|
468
|
+
return {};
|
|
405
469
|
if (expr.kind === SyntaxKind.NewExpression) {
|
|
406
470
|
const identifier = expr.expression;
|
|
407
471
|
const type = identifier?.text;
|
|
408
472
|
if (type) {
|
|
409
|
-
return { type,
|
|
473
|
+
return { defaultValueClass: type, defaultValue: {} };
|
|
410
474
|
}
|
|
475
|
+
} else if (expr.kind === SyntaxKind.PropertyAccessExpression && expr.expression.kind === SyntaxKind.ThisKeyword) {
|
|
476
|
+
return { defaultValueCopyFrom: expr.name?.text };
|
|
411
477
|
}
|
|
478
|
+
return { defaultValue: parseLiteralNode(expr, ctx) };
|
|
479
|
+
}
|
|
480
|
+
function parseLiteralNode(expr, ctx) {
|
|
481
|
+
if (!expr)
|
|
482
|
+
return void 0;
|
|
412
483
|
if (expr.kind === SyntaxKind.StringLiteral)
|
|
413
|
-
return
|
|
484
|
+
return expr.text;
|
|
414
485
|
if (expr.kind === SyntaxKind.FalseKeyword)
|
|
415
|
-
return
|
|
486
|
+
return false;
|
|
416
487
|
if (expr.kind === SyntaxKind.TrueKeyword)
|
|
417
|
-
return
|
|
488
|
+
return true;
|
|
418
489
|
if (expr.kind === SyntaxKind.NumericLiteral)
|
|
419
|
-
return
|
|
490
|
+
return Number.parseFloat(expr.text);
|
|
420
491
|
if (expr.kind === SyntaxKind.ObjectLiteralExpression)
|
|
421
|
-
return
|
|
492
|
+
return parseObjectLiteral(expr, ctx);
|
|
422
493
|
if (expr.kind === SyntaxKind.ArrayLiteralExpression) {
|
|
423
494
|
const defaultValueStr = expr.getText(ctx.sourceFile);
|
|
424
495
|
let defaultValue;
|
|
@@ -429,10 +500,7 @@ function parseLiteralNode(expr, ctx) {
|
|
|
429
500
|
}
|
|
430
501
|
if (!_.isArray(defaultValue))
|
|
431
502
|
throw new Error("Value must be valid array");
|
|
432
|
-
return
|
|
433
|
-
}
|
|
434
|
-
if (expr.kind === SyntaxKind.PropertyAccessExpression && expr.expression.kind === SyntaxKind.ThisKeyword) {
|
|
435
|
-
return { type: "thisReference", value: expr.name?.text };
|
|
503
|
+
return defaultValue;
|
|
436
504
|
}
|
|
437
505
|
throw new Error(`Unexpected property expression: "${expr.getText(ctx.sourceFile)}"`);
|
|
438
506
|
}
|
|
@@ -447,7 +515,7 @@ function parseObjectLiteral(arg, ctx) {
|
|
|
447
515
|
throw new Error(`Unexpected property value: "${p.getText(ctx.sourceFile)}"`);
|
|
448
516
|
const p2 = p;
|
|
449
517
|
const valueExpression = p2.initializer;
|
|
450
|
-
const value = parseLiteralNode(valueExpression, ctx)
|
|
518
|
+
const value = parseLiteralNode(valueExpression, ctx);
|
|
451
519
|
result[name] = value;
|
|
452
520
|
}
|
|
453
521
|
return result;
|