Skip to content

Commit

Permalink
typesafety on jdl to json
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed May 12, 2024
1 parent 30530fe commit 83120ed
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
* @param jdlEntities - the JDL entities to convert.
* @return a map having for keys entity names and for values the corresponding JSON entities.
*/
export function convert(jdlEntities: JDLEntity[]): Map<string, JSONEntity> {
export function convert(jdlEntities?: JDLEntity[] | null): Map<string, JSONEntity> {
if (!jdlEntities) {
throw new Error('JDL entities must be passed to get the basic entity information.');
}
Expand Down
6 changes: 3 additions & 3 deletions jdl/converters/jdl-to-json/jdl-to-json-field-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default { convert };
* @param jdlObject - the JDL object containing entities, fields and enums.
* @return a map having for keys an entity's name and for values its JSON fields.
*/
export function convert(jdlObject: JDLObject): Map<string, JSONField[]> {
export function convert(jdlObject?: JDLObject | null): Map<string, JSONField[]> {
if (!jdlObject) {
throw new Error('A JDL Object must be passed to convert JDL fields to JSON.');
}
Expand Down Expand Up @@ -102,7 +102,7 @@ function getConvertedFieldsForEntity(jdlEntity: JDLEntity, jdlObject: JDLObject)
return convertedEntityFields;
}

function getBlobFieldData(fieldType: string) {
function getBlobFieldData(fieldType: string): { fieldType: 'bytes'; fieldTypeBlobContent: 'image' | 'any' | 'text' } {
const blobFieldData: any = {
fieldType: BYTES,
};
Expand Down Expand Up @@ -136,7 +136,7 @@ function getFieldValidations(jdlField: JDLField) {
return fieldValidations;
}

function getOptionsForField(jdlField) {
function getOptionsForField(jdlField: JDLField) {
const fieldOptions = {
options: {},
};
Expand Down
12 changes: 6 additions & 6 deletions jdl/converters/jdl-to-json/jdl-to-json-option-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type JDLOptionHolder = JDLObject | JDLApplication;
* @param jdlOptionHolder - a JDL object (a JDLObject or a JDLApplication) containing the options.
* @return {Map<String, Object>} a map having for keys entity names and for values the JSON option contents.
*/
export function convert(jdlOptionHolder: JDLOptionHolder) {
export function convert(jdlOptionHolder?: JDLOptionHolder | null): Map<string, any> {
if (!jdlOptionHolder) {
throw new Error('A JDL object or application must be passed to convert JDL options to JSON.');
}
Expand All @@ -60,21 +60,21 @@ export function convert(jdlOptionHolder: JDLOptionHolder) {
return convertedOptionContent;
}

function resolveEntityNamesForEachOption(jdlOptionHolder: JDLOptionHolder) {
function resolveEntityNamesForEachOption(jdlOptionHolder: JDLOptionHolder): void {
jdlOptionHolder.forEachOption(jdlOption => {
if (jdlOption.entityNames.has('*')) {
jdlOption.setEntityNames(jdlOptionHolder.getEntityNames().filter(entityName => !jdlOption.excludedNames.has(entityName)));
}
});
}

function setConvertedOptionContents(jdlOptionHolder: JDLOptionHolder) {
function setConvertedOptionContents(jdlOptionHolder: JDLOptionHolder): void {
jdlOptionHolder.forEachOption(jdlOption => {
setOptionsToEachEntityName(jdlOption);
});
}

function setOptionsToEachEntityName(jdlOption) {
function setOptionsToEachEntityName(jdlOption: AbstractJDLOption): void {
const { key, value } = getJSONOptionKeyAndValue(jdlOption);

jdlOption.entityNames.forEach(entityName => {
Expand Down Expand Up @@ -118,13 +118,13 @@ function getJSONOptionKeyAndValue(jdlOption: AbstractJDLOption): { key: string;
}
}

function preventEntitiesFromBeingSearched(entityNames: string[]) {
function preventEntitiesFromBeingSearched(entityNames: Set<string>) {
entityNames.forEach(entityName => {
setOptionToEntityName({ optionName: 'searchEngine', optionValue: NO_SEARCH_ENGINE }, entityName);
});
}

function setOptionToEntityName(option, entityName: string) {
function setOptionToEntityName(option, entityName: string): void {
const { optionName, optionValue } = option;
const optionContentForEntity = convertedOptionContent.get(entityName) ?? {};
optionContentForEntity[optionName] = optionValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
/* eslint-disable no-unused-expressions */

import { before, it, describe, expect, expect as jestExpect } from 'esmocha';
import { before, it, describe, expect as jestExpect } from 'esmocha';
import { expect } from 'chai';
import JDLRelationship from '../../models/jdl-relationship.js';
import { convert } from './jdl-to-json-relationship-converter.js';
Expand Down
36 changes: 20 additions & 16 deletions jdl/converters/jdl-to-json/jdl-to-json-relationship-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { kebabCase } from 'lodash-es';
import { relationshipOptions, validations } from '../../jhipster/index.js';
import { camelCase, lowerFirst } from '../../utils/string-utils.js';
import JDLRelationship from '../../models/jdl-relationship.js';
import { JSONRelationship } from '../types.js';
import { RelationshipType } from '../../basic-types/relationships.js';

const {
Validations: { REQUIRED },
Expand Down Expand Up @@ -50,13 +52,15 @@ export function convert(jdlRelationships: JDLRelationship[] = [], entityNames: s
return convertedRelationships;
}

function getRelatedRelationships(relationships, entityNames) {
type RelationshipsRelatedToEntity = {
from: JDLRelationship[];
to: JDLRelationship[];
};

function getRelatedRelationships(relationships: JDLRelationship[], entityNames: string[]) {
const relatedRelationships = new Map();
entityNames.forEach(entityName => {
const relationshipsRelatedToEntity: {
from: any[];
to: any[];
} = {
const relationshipsRelatedToEntity: RelationshipsRelatedToEntity = {
from: [],
to: [],
};
Expand All @@ -76,12 +80,12 @@ function getRelatedRelationships(relationships, entityNames) {
return relatedRelationships;
}

function setRelationshipsFromEntity(relatedRelationships, entityName) {
function setRelationshipsFromEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string) {
relatedRelationships.from.forEach(relationshipToConvert => {
const otherSplitField: any = extractField(relationshipToConvert.injectedFieldInTo);
const convertedRelationship: any = {
const convertedRelationship: Partial<JSONRelationship> = {
relationshipSide: 'left',
relationshipType: kebabCase(relationshipToConvert.type),
relationshipType: kebabCase(relationshipToConvert.type) as RelationshipType,
otherEntityName: camelCase(relationshipToConvert.to),
};
if (otherSplitField.relationshipName) {
Expand All @@ -106,7 +110,7 @@ function setRelationshipsFromEntity(relatedRelationships, entityName) {

export const otherRelationshipType = relationshipType => relationshipType.split('-').reverse().join('-');

function setRelationshipsToEntity(relatedRelationships, entityName) {
function setRelationshipsToEntity(relatedRelationships: RelationshipsRelatedToEntity, entityName: string) {
relatedRelationships.to.forEach(relationshipToConvert => {
const otherSplitField = extractField(relationshipToConvert.injectedFieldInFrom);
const convertedRelationship: any = {
Expand Down Expand Up @@ -137,30 +141,30 @@ function setRelationshipsToEntity(relatedRelationships, entityName) {
});
}

function setOptionsForRelationshipSourceSide(relationshipToConvert, convertedRelationship) {
function setOptionsForRelationshipSourceSide(relationshipToConvert: JDLRelationship, convertedRelationship: Partial<JSONRelationship>) {
convertedRelationship.options = convertedRelationship.options || {};
relationshipToConvert.forEachGlobalOption((optionName, optionValue) => {
if (optionName === BUILT_IN_ENTITY) {
convertedRelationship.relationshipWithBuiltInEntity = optionValue;
} else {
convertedRelationship.options[optionName] = optionValue;
convertedRelationship.options![optionName] = optionValue;
}
});
relationshipToConvert.forEachDestinationOption((optionName, optionValue) => {
convertedRelationship.options[optionName] = optionValue;
convertedRelationship.options![optionName] = optionValue;
});
if (Object.keys(convertedRelationship.options).length === 0) {
delete convertedRelationship.options;
}
}

function setOptionsForRelationshipDestinationSide(relationshipToConvert, convertedRelationship) {
function setOptionsForRelationshipDestinationSide(relationshipToConvert: JDLRelationship, convertedRelationship: JSONRelationship) {
convertedRelationship.options = convertedRelationship.options || {};
relationshipToConvert.forEachGlobalOption((optionName, optionValue) => {
convertedRelationship.options[optionName] = optionValue;
convertedRelationship.options![optionName] = optionValue;
});
relationshipToConvert.forEachSourceOption((optionName, optionValue) => {
convertedRelationship.options[optionName] = optionValue;
convertedRelationship.options![optionName] = optionValue;
});
if (Object.keys(convertedRelationship.options).length === 0) {
delete convertedRelationship.options;
Expand All @@ -173,7 +177,7 @@ function setOptionsForRelationshipDestinationSide(relationshipToConvert, convert
* @return{Object} where 'relationshipName' is the relationship name and
* 'otherEntityField' is the other entity field name
*/
function extractField(field) {
function extractField(field?: string | null) {
const splitField: any = {
relationshipName: '',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import BasicEntityConverter from './jdl-to-json-basic-entity-converter.js';
import FieldConverter from './jdl-to-json-field-converter.js';
import RelationshipConverter from './jdl-to-json-relationship-converter.js';
import OptionConverter from './jdl-to-json-option-converter.js';
import JDLObject, { JDLObjectWrapper } from '../../models/jdl-object.js';
import JDLApplication from '../../models/jdl-application.js';
import JSONEntity from '../../jhipster/json-entity.js';

let entities;
let jdlObject;
let entitiesPerApplication;
let entities: Record<string, JSONEntity> | null | undefined;
let jdlObject: JDLObject | null | undefined;
let entitiesPerApplication: Map<string, string[]>;

export default { convert };

Expand All @@ -34,13 +37,13 @@ export default { convert };
* @param {JDLObject} args.jdlObject - the JDLObject to convert to JSON
* @returns {Map} entities that can be exported to JSON
*/
export function convert(args: any = {}) {
export function convert(args: JDLObjectWrapper = {}) {
if (!args.jdlObject) {
throw new Error('The JDL object is mandatory.');
}
init(args);
setEntitiesPerApplication();
if (entitiesPerApplication.size === 0) {
if (entitiesPerApplication.size === 0 && jdlObject) {
const applicationNames = jdlObject.getApplications().map(jdlApplication => jdlApplication.getConfigurationOptionValue('baseName'));
return new Map(applicationNames.map(applicationName => [applicationName, []]));
}
Expand All @@ -55,7 +58,7 @@ export function convert(args: any = {}) {
return entitiesForEachApplication;
}

function init(args) {
function init(args: JDLObjectWrapper): void {
if (jdlObject) {
resetState();
}
Expand All @@ -64,13 +67,13 @@ function init(args) {
entitiesPerApplication = new Map();
}

function resetState() {
function resetState(): void {
jdlObject = null;
entities = null;
}

function setEntitiesPerApplication() {
jdlObject.forEachApplication(jdlApplication => {
function setEntitiesPerApplication(): void {
jdlObject?.forEachApplication((jdlApplication: JDLApplication) => {
const entityNames = jdlApplication.getEntityNames();
if (entityNames.length === 0) {
return;
Expand All @@ -80,45 +83,45 @@ function setEntitiesPerApplication() {
});
}

function setBasicEntityInformation() {
const convertedEntities = BasicEntityConverter.convert(jdlObject.getEntities());
function setBasicEntityInformation(): void {
const convertedEntities = BasicEntityConverter.convert(jdlObject?.getEntities());
convertedEntities.forEach((jsonEntity, entityName) => {
entities[entityName] = jsonEntity;
entities![entityName] = jsonEntity;
});
}

function setFields() {
function setFields(): void {
const convertedFields = FieldConverter.convert(jdlObject);
convertedFields.forEach((entityFields, entityName) => {
entities[entityName].addFields(entityFields);
entities![entityName].addFields(entityFields);
});
}

function setRelationships() {
const convertedRelationships = RelationshipConverter.convert(jdlObject.getRelationships(), jdlObject.getEntityNames());
function setRelationships(): void {
const convertedRelationships = RelationshipConverter.convert(jdlObject?.getRelationships(), jdlObject?.getEntityNames());
convertedRelationships.forEach((entityRelationships, entityName) => {
entities[entityName].addRelationships(entityRelationships);
entities![entityName].addRelationships(entityRelationships);
});
}

function setApplicationToEntities() {
jdlObject.forEachApplication(jdlApplication => {
function setApplicationToEntities(): void {
jdlObject?.forEachApplication((jdlApplication: JDLApplication) => {
const baseName = jdlApplication.getConfigurationOptionValue('baseName');
jdlApplication.forEachEntityName(entityName => {
if (!entities[entityName]) {
jdlApplication.forEachEntityName((entityName: string) => {
if (!entities![entityName]) {
return;
}
entities[entityName].applications.push(baseName);
entities![entityName].applications.push(baseName);
});
});
}

function setOptions(entitiesForEachApplication) {
const convertedOptionContents = OptionConverter.convert(jdlObject);
convertedOptionContents.forEach((optionContent, entityName) => {
entities[entityName].setOptions(optionContent);
entities![entityName].setOptions(optionContent);
});
jdlObject.forEachApplication(jdlApplication => {
jdlObject?.forEachApplication(jdlApplication => {
const convertedOptionContentsForApplication = OptionConverter.convert(jdlApplication);
const applicationName = jdlApplication.getConfigurationOptionValue('baseName');
const applicationEntities = entitiesForEachApplication.get(applicationName);
Expand All @@ -135,8 +138,8 @@ function getEntitiesForEachApplicationMap() {
const entitiesForEachApplication = new Map();
entitiesPerApplication.forEach((entityNames, applicationName) => {
const entitiesInObject = entityNames
.filter(entityName => !!entities[entityName])
.map(entityName => entities[entityName])
.filter(entityName => !!entities![entityName])
.map(entityName => entities![entityName])
.reduce((accumulator, currentEntity) => {
return {
...accumulator,
Expand All @@ -156,7 +159,7 @@ function formatEntitiesForEachApplication(entitiesForEachApplication) {
}

function addApplicationsWithoutEntities(entitiesForEachApplication) {
jdlObject.forEachApplication(jdlApplication => {
jdlObject?.forEachApplication(jdlApplication => {
if (jdlApplication.getEntityNames().length === 0) {
entitiesForEachApplication.set(jdlApplication.getConfigurationOptionValue('baseName'), []);
}
Expand Down
Loading

0 comments on commit 83120ed

Please sign in to comment.