Skip to content
Open
Show file tree
Hide file tree
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
154 changes: 142 additions & 12 deletions lib/codegen/fromcto/markdown/markdownvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,56 @@ class MarkdownVisitor extends BaseVisitor {
* @return {Object} the result of visiting or null
* @private
*/
visitClassDeclaration(classDeclaration, parameters) {
visitAssetDeclaration(classDeclaration, parameters) {
return this.visitClassDeclaration(classDeclaration, parameters, 'Asset');
}

/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitParticipantDeclaration(classDeclaration, parameters) {
return this.visitClassDeclaration(classDeclaration, parameters, 'Participant');
}

/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitTransactionDeclaration(classDeclaration, parameters) {
return this.visitClassDeclaration(classDeclaration, parameters, 'Transaction');
}

/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitEventDeclaration(classDeclaration, parameters) {
return this.visitClassDeclaration(classDeclaration, parameters, 'Event');
}

/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @param {string} [type] - the declaration type (e.g. Concept, Asset, Participant, Transaction, Event)
* @return {Object} the result of visiting or null
* @private
*/
visitClassDeclaration(classDeclaration, parameters, type = 'Concept') {
parameters.stack.push(classDeclaration.getFullyQualifiedName());

parameters.fileWriter.writeLine(0, '');
parameters.fileWriter.writeLine(0, `### ${classDeclaration.getName()} (Concept)`);
parameters.fileWriter.writeLine(0, `### ${classDeclaration.getName()} (${type})`);
parameters.fileWriter.writeLine(0, '');

const descriptionDecorator = classDeclaration.getDecorator('description');
Expand All @@ -140,7 +185,7 @@ class MarkdownVisitor extends BaseVisitor {
parameters.fileWriter.writeLine(0, '');

if (classDeclaration.getProperties().length > 0) {
parameters.fileWriter.writeLine(0, '| Field | Type | Required | Identifier | Reference | Inherited | Description | Contraints | ');
parameters.fileWriter.writeLine(0, '| Field | Type | Required | Identifier | Reference | Inherited | Description | Constraints | ');
parameters.fileWriter.writeLine(0, '| ------ | ------------------- | --- | --- | --- | --- |------------ | --------- |');
classDeclaration.getProperties().forEach((property) => {
property.accept(this, parameters);
Expand Down Expand Up @@ -203,6 +248,91 @@ class MarkdownVisitor extends BaseVisitor {
parameters.fileWriter.writeLine(0, `| ${enumValueDeclaration.getName()} |${descriptionDecorator?.getArguments()[0] || ''} |`);
}

/**
* Visitor design pattern
* @param {MapDeclaration} mapDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {
parameters.stack.push(mapDeclaration.getFullyQualifiedName());

parameters.fileWriter.writeLine(0, '');
parameters.fileWriter.writeLine(0, `### ${mapDeclaration.getName()} (Map)`);
parameters.fileWriter.writeLine(0, '');

const descriptionDecorator = mapDeclaration.getDecorator('description');
if (descriptionDecorator && descriptionDecorator.getArguments().length > 0) {
parameters.fileWriter.writeLine(0, descriptionDecorator?.getArguments()[0] || '');
}

const keyType = mapDeclaration.getKey().getType();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit · design

Map key and value types are rendered with getType(), which gives the short name: the snapshot shows <code>Employee</code> and <code>SSN</code>. Field types in the same document are fully qualified (org.acme.hr@1.0.0.Employee). For a map whose key or value type is imported, readers can't tell which namespace it comes from. Consider resolving non-primitive names to their fully qualified form, for example through the model file's getFullyQualifiedTypeName, so maps match the class tables.

const valueType = mapDeclaration.getValue().getType();

parameters.fileWriter.writeLine(0, '| Key Type | Value Type |');
parameters.fileWriter.writeLine(0, '| -------- | ---------- |');
parameters.fileWriter.writeLine(0, `| <code>${keyType}</code> | <code>${valueType}</code> |`);
parameters.fileWriter.writeLine(0, '');

mapDeclaration.getDecorators().forEach(decorator => decorator.accept(this, parameters));

parameters.stack.pop();
return null;
}

/**
* Visitor design pattern
* @param {ScalarDeclaration} scalarDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitScalarDeclaration(scalarDeclaration, parameters) {
parameters.stack.push(scalarDeclaration.getFullyQualifiedName());

parameters.fileWriter.writeLine(0, '');
parameters.fileWriter.writeLine(0, `### ${scalarDeclaration.getName()} (Scalar)`);
parameters.fileWriter.writeLine(0, '');

const descriptionDecorator = scalarDeclaration.getDecorator('description');
if (descriptionDecorator && descriptionDecorator.getArguments().length > 0) {
parameters.fileWriter.writeLine(0, descriptionDecorator?.getArguments()[0] || '');
}

let constraints = [];
const validator = scalarDeclaration.getValidator?.();
if (validator) {
if (validator.getMinLength?.() !== undefined && typeof validator.getMinLength() === 'number') {
constraints.push(`Minimum Length: ${validator.getMinLength()}`);
}
if (validator.getMaxLength?.() !== undefined && typeof validator.getMaxLength() === 'number') {
constraints.push(`Maximum Length: ${validator.getMaxLength()}`);
}
if (validator.getRegex?.()) {
constraints.push(`Matches Regular Expression: \`${validator.getRegex()}\``);
}
if (validator.getLowerBound?.() !== undefined && typeof validator.getLowerBound() === 'number') {
constraints.push(`Minimum Value: ${validator.getLowerBound()}`);
}
if (validator.getUpperBound?.() !== undefined && typeof validator.getUpperBound() === 'number') {
constraints.push(`Maximum Value: ${validator.getUpperBound()}`);
}
}

const defaultValue = scalarDeclaration.getDefaultValue?.() ? `<code>${scalarDeclaration.getDefaultValue()}</code>` : '';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

should-fix · correctness

visitScalarDeclaration uses a truthiness test to decide whether to print the default value: scalarDeclaration.getDefaultValue?.() ? ... : ''. ScalarDeclaration.getDefaultValue() returns null when no default is set, and otherwise returns the value, which can be 0, 0.0 or false. So scalar Count extends Integer default 0 or scalar Flag extends Boolean default false gets an empty Default Value cell, as if it had no default. Suggested fix: const dv = scalarDeclaration.getDefaultValue?.(); const defaultValue = dv !== null && dv !== undefined ? ${dv} : '';. Please also add a unit test with a falsy default (e.g. getDefaultValue.returns(0)), since the current scalar test only covers a non-empty string default.


parameters.fileWriter.writeLine(0, '| Type | Default Value | Constraints |');
parameters.fileWriter.writeLine(0, '| ---- | ------------- | ----------- |');
parameters.fileWriter.writeLine(0, `| <code>${scalarDeclaration.getType()}</code> | ${defaultValue} | ${constraints.join('<br>')} |`);
parameters.fileWriter.writeLine(0, '');

scalarDeclaration.getDecorators().forEach(decorator => decorator.accept(this, parameters));

parameters.stack.pop();
return null;
}

/**
* Visitor design pattern
* @param {Field} field - the object being visited
Expand Down Expand Up @@ -237,38 +367,38 @@ class MarkdownVisitor extends BaseVisitor {

const validator = field.getValidator?.();

let contraints = [];
let constraints = [];
if(validator) {
if(validator.getMinLength?.() !== undefined && typeof validator.getMinLength() === 'number') {
contraints.push(`Minimum Length: ${validator.getMinLength()}`);
constraints.push(`Minimum Length: ${validator.getMinLength()}`);
}
if(validator.getMaxLength?.() !== undefined && typeof validator.getMaxLength() === 'number') {
contraints.push(`Maximum Length: ${validator.getMaxLength()}`);
constraints.push(`Maximum Length: ${validator.getMaxLength()}`);
}
if (validator.getRegex?.()) {
contraints.push(`Matches Regular Expression: \`${validator.getRegex()}\``);
constraints.push(`Matches Regular Expression: \`${validator.getRegex()}\``);
}
if(validator.getLowerBound?.() !== undefined && typeof validator.getLowerBound() === 'number') {
contraints.push(`Minimum Value: ${validator.getLowerBound()}`);
constraints.push(`Minimum Value: ${validator.getLowerBound()}`);
}
if(validator.getUpperBound?.() !== undefined && typeof validator.getUpperBound() === 'number') {
contraints.push(`Maximum Value: ${validator.getUpperBound()}`);
constraints.push(`Maximum Value: ${validator.getUpperBound()}`);
}
}

const sizeValidator = field.getSizeValidator?.();
if (sizeValidator) {
if (sizeValidator.getMinSize() !== null) {
contraints.push(`Minimum Size: ${sizeValidator.getMinSize()}`);
constraints.push(`Minimum Size: ${sizeValidator.getMinSize()}`);
}
if (sizeValidator.getMaxSize() !== null) {
contraints.push(`Maximum Size: ${sizeValidator.getMaxSize()}`);
constraints.push(`Maximum Size: ${sizeValidator.getMaxSize()}`);
}
}

const isInherited = field.getParent().getFullyQualifiedName() !== parameters.stack[parameters.stack.length-1];

parameters.fileWriter.writeLine(0, `| ${field.getName()} | <code>${type}</code> | ${!field.isOptional() ? '✔️' : ''} | ${isIdentifier ? '✔️' : ''} | ${field.isRelationship?.() ? '✔️' : ''} | ${isInherited ? '✔️' : ''} | ${descriptionDecorator?.getArguments()[0] || ''} | ${contraints.join('<br>')} |`);
parameters.fileWriter.writeLine(0, `| ${field.getName()} | <code>${type}</code> | ${!field.isOptional() ? '✔️' : ''} | ${isIdentifier ? '✔️' : ''} | ${field.isRelationship?.() ? '✔️' : ''} | ${isInherited ? '✔️' : ''} | ${descriptionDecorator?.getArguments()[0] || ''} | ${constraints.join('<br>')} |`);
}

/**
Expand Down
Loading
Loading