Skip to content

Commit fcd83c6

Browse files
authored
HCK-9559: add enums FE (#37)
* add enums FE * rename const
1 parent c8a6dc1 commit fcd83c6

File tree

9 files changed

+181
-39
lines changed

9 files changed

+181
-39
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const GRAPHQL_SCHEMA_SCRIPT_INDENT = ' ';
2+
3+
module.exports = {
4+
GRAPHQL_SCHEMA_SCRIPT_INDENT,
5+
};

forward_engineering/helpers/descriptionsHelper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ function getStatementDescription({ description }) {
1111
return '';
1212
}
1313

14-
const isMultiLine = description.includes('\n');
14+
const trimmedDescription = description.trim();
15+
const escapedDescription = trimmedDescription.replace(/"/g, '\\"');
16+
const isMultiLine = escapedDescription.includes('\n');
1517

1618
// Format the description based on whether it is multi-line or single-line
17-
const formattedDescription = isMultiLine ? `"""\n${description}\n"""` : `"${description}"`;
19+
const formattedDescription = isMultiLine ? `"""\n${escapedDescription}\n"""` : `"${escapedDescription}"`;
1820

1921
return formattedDescription;
2022
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { commentOutDeactivatedRootFEStatement } = require('./deactivatedItemsHelper');
2+
const { getStatementDescription } = require('./descriptionsHelper');
3+
const { addIndentToStatement } = require('./feStatementIndentHelper');
4+
5+
/**
6+
* @typedef { import("../types/types").FEStatement } FEStatement
7+
*/
8+
9+
/**
10+
* Combines the description and statement, and comments out the statement if it is deactivated.
11+
* Adds formatted nested statements if they exist.
12+
*
13+
* @param {Object} param0
14+
* @param {FEStatement} param0.feStatement - The forward engineering statement object.
15+
* @returns {string} - The final formatted statement.
16+
*/
17+
function formatFEStatement({ feStatement }) {
18+
const {
19+
statement,
20+
description,
21+
isActivated = true,
22+
nestedStatements,
23+
useCurlyBracketsForNestedStatements = true,
24+
} = feStatement;
25+
let result = '';
26+
27+
if (description?.trim()) {
28+
const formattedDescription = getStatementDescription({ description });
29+
result += `${formattedDescription}\n`;
30+
}
31+
32+
result += statement;
33+
34+
if (nestedStatements?.length > 0) {
35+
const formattedNestedStatements = nestedStatements
36+
.map(nestedStatement =>
37+
addIndentToStatement({ statement: formatFEStatement({ feStatement: nestedStatement }) }),
38+
)
39+
.join('\n');
40+
41+
if (useCurlyBracketsForNestedStatements) {
42+
result += ` {\n${formattedNestedStatements}\n}`;
43+
} else {
44+
result += `\n${formattedNestedStatements}`;
45+
}
46+
}
47+
48+
if (!isActivated) {
49+
result = commentOutDeactivatedRootFEStatement({ statement: result, isActivated });
50+
}
51+
52+
return result;
53+
}
54+
55+
module.exports = {
56+
formatFEStatement,
57+
};

forward_engineering/helpers/feStatementHelper.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { GRAPHQL_SCHEMA_SCRIPT_INDENT } = require('../constants/feScriptConstants');
2+
3+
/**
4+
* Adds indentation to each line of a given statement.
5+
*
6+
* @param {Object} param0
7+
* @param {string} param0.statement - The statement to which indentation will be added.
8+
* @param {string} [param0.indent=GRAPHQL_SCHEMA_SCRIPT_INDENT] - The indentation string to use. Defaults to GRAPHQL_INDENT.
9+
* @returns {string} - The indented statement.
10+
*/
11+
function addIndentToStatement({ statement, indent = GRAPHQL_SCHEMA_SCRIPT_INDENT }) {
12+
return statement
13+
.split('\n')
14+
.map(line => `${indent}${line}`)
15+
.join('\n');
16+
}
17+
18+
module.exports = {
19+
addIndentToStatement,
20+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Joins an array of statements into a single line.
3+
* Trims each statement, filters out empty statements, and joins them with a space.
4+
*
5+
* @param {Object} param0
6+
* @param {string[]} param0.statements - The array of statements to join.
7+
* @returns {string} - The joined statements as a single line.
8+
*/
9+
function joinInlineStatements({ statements }) {
10+
return statements
11+
.map(statement => statement.trim())
12+
.filter(statement => typeof statement === 'string' && statement.length > 0)
13+
.join(' ');
14+
}
15+
16+
module.exports = {
17+
joinInlineStatements,
18+
};

forward_engineering/mappers/enums.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @typedef { import("../types/types").FEStatement } FEStatement
3+
*/
4+
5+
const { joinInlineStatements } = require('../helpers/feStatementJoinHelper');
6+
7+
/**
8+
* @typedef {Object} EnumValue
9+
* @property {string} value - The name of the enum value.
10+
* @property {string} description - The description of the enum value.
11+
* @property {Object} valueDirectives - The directives of the enum value.
12+
*/
13+
14+
/**
15+
* @typedef {Object} EnumDefinition
16+
* @property {string} description - The description of the enum.
17+
* @property {boolean} isActivated - Indicates if the enum is activated.
18+
* @property {Object} typeDirectives - The directives of the enum.
19+
* @property {EnumValue[]} enumValues - The values of the emu,.
20+
*/
21+
22+
/**
23+
* @typedef {Object.<string, EnumDefinition>} EnumDefinitions
24+
*/
25+
26+
/**
27+
* Gets the enums as an array of FEStatements.
28+
*
29+
* @param {Object} param0
30+
* @param {EnumDefinitions} param0.enumsDefinitions - The enums object.
31+
* @returns {FEStatement[]}
32+
*/
33+
function getEnums({ enumsDefinitions }) {
34+
return Object.entries(enumsDefinitions).map(([name, enumDefinition]) => mapEnum({ name, enumDefinition }));
35+
}
36+
37+
/**
38+
* Maps a enum to an FEStatement.
39+
*
40+
* @param {Object} param0
41+
* @param {string} param0.name - The name of the enum.
42+
* @param {EnumDefinition} param0.enumDefinition - The enum definition object.
43+
* @returns {FEStatement}
44+
*/
45+
function mapEnum({ name, enumDefinition }) {
46+
return {
47+
statement: joinInlineStatements({ statements: [`enum ${name}`] }), // TODO: add directives
48+
description: enumDefinition.description,
49+
isActivated: enumDefinition.isActivated,
50+
nestedStatements: mapEnumValues({ enumValues: enumDefinition.enumValues }),
51+
};
52+
}
53+
54+
/**
55+
* Maps the enum values to an array of FEStatement.
56+
*
57+
* @param {Object} param0
58+
* @param {EnumValue[]} param0.enumValues - The enum values.
59+
* @returns {FEStatement[]}
60+
*/
61+
function mapEnumValues({ enumValues = [] }) {
62+
return enumValues.map(({ value, description }) => ({
63+
statement: joinInlineStatements({ statements: [value] }), // TODO: add directives
64+
description: description,
65+
}));
66+
}
67+
68+
module.exports = {
69+
getEnums,
70+
};

forward_engineering/mappers/typeDefinitions.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { formatFEStatement } = require('../helpers/feStatementHelper');
1+
const { formatFEStatement } = require('../helpers/feStatementFormatHelper');
22
const { getCustomScalars } = require('./customScalars');
3+
const { getEnums } = require('./enums');
34

45
/**
56
* Gets the type definition statements from model definitions.
@@ -12,8 +13,9 @@ function getTypeDefinitionStatements({ modelDefinitions }) {
1213
const customScalars = getCustomScalars({
1314
customScalars: getModelDefinitionsBySubtype({ modelDefinitions, subtype: 'scalar' }),
1415
});
16+
const enums = getEnums({ enumsDefinitions: getModelDefinitionsBySubtype({ modelDefinitions, subtype: 'enum' }) });
1517

16-
const typeDefinitions = [...customScalars]; // TODO: Add other types here
18+
const typeDefinitions = [...customScalars, ...enums];
1719
const formattedTypeDefinitions = typeDefinitions
1820
.map(typeDefinition => formatFEStatement({ feStatement: typeDefinition }))
1921
.join('\n\n');

forward_engineering/types/types.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export type FEStatement = {
22
statement: string;
33
description: string;
4-
isActivated: boolean;
4+
isActivated?: boolean;
5+
nestedStatements?: FEStatement[];
6+
useCurlyBracketsForNestedStatements?: boolean;
57
}

0 commit comments

Comments
 (0)