forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumConstrainer.ts
109 lines (104 loc) · 3.45 KB
/
EnumConstrainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ConstrainedEnumModel, EnumModel } from '../../../models';
import {
NO_NUMBER_START_CHAR,
NO_DUPLICATE_ENUM_KEYS,
NO_EMPTY_VALUE,
NO_RESERVED_KEYWORDS
} from '../../../helpers/Constraints';
import {
FormatHelpers,
EnumKeyConstraint,
EnumValueConstraint
} from '../../../helpers';
import { isReservedTemplateKeyword } from '../Constants';
import {
TemplateEnumKeyConstraint,
TemplateEnumValueConstraint
} from '../TemplateGenerator';
export type ModelEnumKeyConstraints = {
NO_SPECIAL_CHAR: (value: string) => string;
NO_NUMBER_START_CHAR: (value: string) => string;
NO_DUPLICATE_KEYS: (
constrainedEnumModel: ConstrainedEnumModel,
enumModel: EnumModel,
value: string,
namingFormatter: (value: string) => string
) => string;
NO_EMPTY_VALUE: (value: string) => string;
NAMING_FORMATTER: (value: string) => string;
NO_RESERVED_KEYWORDS: (value: string) => string;
};
export const DefaultEnumKeyConstraints: ModelEnumKeyConstraints = {
NO_SPECIAL_CHAR: (value: string) => {
//Exclude ` ` because it gets formatted by NAMING_FORMATTER
//Exclude '_' because they are allowed as enum keys
return FormatHelpers.replaceSpecialCharacters(value, {
exclude: [' ', '_'],
separator: '_'
});
},
NO_NUMBER_START_CHAR,
NO_DUPLICATE_KEYS: NO_DUPLICATE_ENUM_KEYS,
NO_EMPTY_VALUE,
NAMING_FORMATTER: FormatHelpers.toConstantCase,
NO_RESERVED_KEYWORDS: (value: string) => {
return NO_RESERVED_KEYWORDS(value, isReservedTemplateKeyword);
}
};
/**
* Default constraint logic for Template, which converts the enum key into a key that is compatible with Template
*/
export function defaultEnumKeyConstraints(
customConstraints?: Partial<ModelEnumKeyConstraints>
): TemplateEnumKeyConstraint {
const constraints = { ...DefaultEnumKeyConstraints, ...customConstraints };
return ({ enumKey, enumModel, constrainedEnumModel }) => {
let constrainedEnumKey = enumKey;
constrainedEnumKey = constraints.NO_SPECIAL_CHAR(constrainedEnumKey);
constrainedEnumKey = constraints.NO_NUMBER_START_CHAR(constrainedEnumKey);
constrainedEnumKey = constraints.NO_EMPTY_VALUE(constrainedEnumKey);
constrainedEnumKey = constraints.NO_RESERVED_KEYWORDS(constrainedEnumKey);
//If the enum key has been manipulated, lets make sure it don't clash with existing keys
if (constrainedEnumKey !== enumKey) {
constrainedEnumKey = constraints.NO_DUPLICATE_KEYS(
constrainedEnumModel,
enumModel,
constrainedEnumKey,
constraints.NAMING_FORMATTER!
);
}
constrainedEnumKey = constraints.NAMING_FORMATTER(constrainedEnumKey);
return constrainedEnumKey;
};
}
/**
* Convert the enum value to a value that is compatible with Template
*/
export function defaultEnumValueConstraints(): TemplateEnumValueConstraint {
return ({ enumValue }) => {
let constrainedEnumValue = enumValue;
switch (typeof enumValue) {
case 'string':
case 'boolean':
constrainedEnumValue = `"${enumValue}"`;
break;
case 'bigint':
case 'number': {
constrainedEnumValue = enumValue;
break;
}
case 'object': {
constrainedEnumValue = `"${JSON.stringify(enumValue).replace(
/"/g,
'\\"'
)}"`;
break;
}
default: {
constrainedEnumValue = `"${JSON.stringify(enumValue)}"`;
}
}
return constrainedEnumValue;
};
}