Skip to content

Commit 0813fc4

Browse files
committed
internal: refactor discriminator
1 parent fce449c commit 0813fc4

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/configuration.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ class CodeGenConfig {
277277
*/
278278
InterfaceDynamicField: (key, value) => `[key: ${key}]: ${value}`,
279279

280+
/**
281+
* EnumName.EnumKey
282+
*/
280283
EnumUsageKey: (enumStruct, key) => `${enumStruct}.${key}`,
281284
/**
282285
* $A1 = $A2

src/schema-parser/base-schema-parsers/discriminator.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { MonoSchemaParser } = require('../mono-schema-parser');
44

55
class DiscriminatorSchemaParser extends MonoSchemaParser {
66
parse() {
7+
const ts = this.config.Ts;
78
const { discriminator, ...noDiscriminatorSchema } = this.schema;
89

910
if (!discriminator.mapping) {
@@ -27,7 +28,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
2728
abstractSchemaStruct,
2829
});
2930

30-
const schemaContent = this.config.Ts.IntersectionType(
31+
const schemaContent = ts.IntersectionType(
3132
[
3233
abstractSchemaStruct?.content,
3334
discriminatorSchemaStruct?.content,
@@ -40,7 +41,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
4041
$parsedSchema: true,
4142
schemaType: SCHEMA_TYPES.COMPLEX,
4243
type: SCHEMA_TYPES.PRIMITIVE,
43-
typeIdentifier: this.config.Ts.Keyword.Type,
44+
typeIdentifier: ts.Keyword.Type,
4445
name: this.typeName,
4546
description: this.schemaFormatters.formatDescription(
4647
this.schema.description,
@@ -50,6 +51,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
5051
}
5152

5253
createDiscriminatorSchema = ({ skipMappingType, abstractSchemaStruct }) => {
54+
const ts = this.config.Ts;
55+
5356
const refPath = this.schemaComponentsMap.createRef([
5457
'components',
5558
'schemas',
@@ -87,10 +90,10 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
8790
mappingTypeName,
8891
]),
8992
),
90-
content: this.config.Ts.IntersectionType([
91-
this.config.Ts.ObjectWrapper(
92-
this.config.Ts.TypeField({
93-
key: this.config.Ts.StringValue(discriminator.propertyName),
93+
content: ts.IntersectionType([
94+
ts.ObjectWrapper(
95+
ts.TypeField({
96+
key: ts.StringValue(discriminator.propertyName),
9497
value: 'Key',
9598
}),
9699
),
@@ -112,18 +115,15 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
112115

113116
const mappingUsageKey =
114117
mappingPropertySchemaEnumKeysMap[mappingKey] ||
115-
this.config.Ts.StringValue(mappingKey);
118+
ts.StringValue(mappingKey);
116119

117120
if (ableToCreateMappingType) {
118-
return this.config.Ts.TypeWithGeneric(mappingTypeName, [
119-
mappingUsageKey,
120-
content,
121-
]);
121+
return ts.TypeWithGeneric(mappingTypeName, [mappingUsageKey, content]);
122122
} else {
123-
return this.config.Ts.ExpressionGroup(
124-
this.config.Ts.IntersectionType([
125-
this.config.Ts.ObjectWrapper(
126-
this.config.Ts.TypeField({
123+
return ts.ExpressionGroup(
124+
ts.IntersectionType([
125+
ts.ObjectWrapper(
126+
ts.TypeField({
127127
key: discriminator.propertyName,
128128
value: mappingUsageKey,
129129
}),
@@ -151,9 +151,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
151151

152152
if (skipMappingType) return null;
153153

154-
const content = this.config.Ts.ExpressionGroup(
155-
this.config.Ts.UnionType(mappingContents),
156-
);
154+
const content = ts.ExpressionGroup(ts.UnionType(mappingContents));
157155

158156
return {
159157
content,
@@ -164,6 +162,8 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
164162
abstractSchemaStruct,
165163
discPropertyName,
166164
}) => {
165+
const ts = this.config.Ts;
166+
167167
let mappingPropertySchemaEnumKeysMap = {};
168168
let mappingPropertySchema = _.get(
169169
abstractSchemaStruct?.component?.rawTypeData,
@@ -183,7 +183,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
183183
(acc, key, index) => {
184184
const enumKey =
185185
mappingPropertySchema.rawTypeData.$parsed.content[index].key;
186-
acc[key] = this.config.Ts.EnumUsageKey(
186+
acc[key] = ts.EnumUsageKey(
187187
mappingPropertySchema.rawTypeData.$parsed.typeName,
188188
enumKey,
189189
);
@@ -284,12 +284,13 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
284284
};
285285

286286
createComplexSchemaStruct = () => {
287+
const ts = this.config.Ts;
287288
const complexType = this.schemaUtils.getComplexType(this.schema);
288289

289290
if (complexType === SCHEMA_TYPES.COMPLEX_UNKNOWN) return null;
290291

291292
return {
292-
content: this.config.Ts.ExpressionGroup(
293+
content: ts.ExpressionGroup(
293294
this.schemaParser._complexSchemaParsers[complexType](this.schema),
294295
),
295296
};

0 commit comments

Comments
 (0)