Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 55 additions & 39 deletions lib/jdl/core/models/jdl-relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,58 +114,74 @@ export default class JDLRelationship implements JDLRelationshipModel {
});
}

// TODO: refactor this function
toString() {
let string = `relationship ${this.type} {\n `;

if (this.commentInFrom) {
string += `/**\n${this.commentInFrom
.split('\n')
.map(line => ` * ${line}\n`)
.join('')} */\n `;
}
const sourceOptions = this.options.source;
if (Object.keys(sourceOptions).length !== 0) {
Object.keys(sourceOptions).forEach(name => {
const value = sourceOptions[name];
name = upperFirst(name);
string += `@${name}${value != null && value !== true ? `(${value}) ` : ' '}`;
});
}
string += `${this.from}`;
if (this.injectedFieldInFrom) {
string += `{${this.injectedFieldInFrom}${this.isInjectedFieldInFromRequired ? ` ${REQUIRED}` : ''}}`;
string += this.formatComment(this.commentInFrom);
}

string += this.formatOptions(this.options.source);

string += this.formatEntityWithField(this.from, this.injectedFieldInFrom, this.isInjectedFieldInFromRequired);

string += ' to';

if (this.commentInTo) {
string += `\n /**\n${this.commentInTo
.split('\n')
.map(line => ` * ${line}\n`)
.join('')} */\n `;
string += `\n ${this.formatComment(this.commentInTo)}`;
} else {
string += ' ';
}
const destinationOptions = this.options.destination;
if (Object.keys(destinationOptions).length !== 0) {
Object.keys(destinationOptions).forEach(name => {
const value = destinationOptions[name];
name = upperFirst(name);
string += `@${name}${value != null && value !== true ? `(${value}) ` : ' '}`;
});

string += this.formatOptions(this.options.destination);

string += this.formatEntityWithField(this.to, this.injectedFieldInTo, this.isInjectedFieldInToRequired);

string += this.formatGlobalOptions();

string += '\n}';

return string.replace(/ \n/g, '\n').replace(/[ ]{4}/g, ' ');
}

private formatComment(comment: string | null | undefined): string {
if (!comment) {
return '';
}
string += `${this.to}`;
if (this.injectedFieldInTo) {
string += `{${this.injectedFieldInTo}${this.isInjectedFieldInToRequired ? ` ${REQUIRED}` : ''}}`;
return `/**\n${comment
.split('\n')
.map(line => ` * ${line}\n`)
.join('')} */\n `;
}

private formatOptions(options: Record<string, any>): string {
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter type Record<string, any> is too permissive and loses type safety. Consider defining a more specific interface for the options object to improve type checking and API clarity.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

difficult because

options: { global: Record<string, any>; source: Record<string, any>; destination: Record<string, any> };

it will require a big refacto

if (Object.keys(options).length === 0) {
return '';
}
return Object.keys(options)
.map(name => {
const value = options[name];
const capitalizedName = upperFirst(name);
return `@${capitalizedName}${value != null && value !== true ? `(${value}) ` : ' '}`;
})
.join('');
}

private formatGlobalOptions(): string {
const globalOptions = this.options.global;
if (Object.keys(globalOptions).length !== 0) {
string += ' with ';
Object.keys(globalOptions).forEach(name => {
string += `${name}, `;
});
string = string.substring(0, string.length - 2);
if (Object.keys(globalOptions).length === 0) {
return '';
}
string += '\n}';
return string.replace(/ \n/g, '\n').replace(/[ ]{4}/g, ' ');
const optionsString = Object.keys(globalOptions).join(', ');
return ` with ${optionsString}`;
}

private formatEntityWithField(entityName: string, injectedField: string | null | undefined, isRequired: boolean): string {
let result = entityName;
if (injectedField) {
result += `{${injectedField}${isRequired ? ` ${REQUIRED}` : ''}}`;
}
return result;
}
}

Expand Down
Loading