Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
feat: support plantUML classDiagram syntax (#13)
Browse files Browse the repository at this point in the history
* feat: support plantUML syntax

* chore: update readme

* refactor: change if to switch statement

* docs: extend jsdoc with available options

* refactor: switch statement

* chore: extend jsodc
  • Loading branch information
arjungarg07 authored Jun 17, 2021
1 parent a81009b commit 0abf06e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ const defaultOutput = appRelationsDiscovery.getRelations(...docs);
- For mermaid Flowchart
```javascript
const mermaidFlowchart = appRelationsDiscovery.getRelations(...docs,{syntax:'mermaid'});
```

- For plantUML classDiagram
```javascript
const plantUMLClassDiagram = appRelationsDiscovery.getRelations(...docs,{syntax:'plantUML'});
```
16 changes: 11 additions & 5 deletions lib/appRelationsDiscovery.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const getMermaidFlowchart = require('./getMermaidFlowchart');
const getPlantUMLDiagram = require('./getPlantUMLDiagram');
const {validate} = require('./utils');

const supportedSyntax = ['default','mermaid'];
const supportedSyntax = ['default','mermaid','plantUML'];

const defaultOptions = {
syntax: 'default',
Expand All @@ -11,7 +12,8 @@ const defaultOptions = {
* Validates and analyzes a list of AsyncAPI documents and get applications described by those files
*
* @param {Array} asyncApiDocs An array of asyncApiDocuments
* @param {Object} options for getting a relations
* @param {Object} [options]
* @param {('default'|'mermaid'|'plantUML')} [options.syntax] syntax in which the relation will be generated.
* @returns {Promise<DiscoveredRelations>} Relations between documents
*/
// eslint-disable-next-line sonarjs/cognitive-complexity
Expand Down Expand Up @@ -74,10 +76,14 @@ async function getRelations(asyncApiDocs, { syntax } = defaultOptions) {
};
}
});
if (syntax === 'default')
return metrics;
if (syntax === 'mermaid')
switch (syntax) {
case 'mermaid':
return getMermaidFlowchart(metrics);
case 'plantUML':
return getPlantUMLDiagram(metrics);
default:
return metrics;
}
};

module.exports = {getRelations};
30 changes: 30 additions & 0 deletions lib/getPlantUMLDiagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Generates plantUML class diagram from default output syntax
*
* @param {Object} metrics Relations between AsyncAPI docs in default output syntax
* @returns {String} class diagram following plantUML syntax
*/
function getPlantUMLDiagram(metrics) {
let i=1;
let plantUMLSyntax = '';
metrics.forEach((relations,server) => {
const [url, protocol] = server.split(',');
plantUMLSyntax+= `\nclass server${i} { \n url: ${url} \n protocol: ${protocol}\n}`;

relations.forEach((operations, channel) => {
operations.sub.forEach((metadata,subscriber) => {
const service = subscriber.replace(/\s/g,'');
plantUMLSyntax+=`\n${service} --|> server${i}:${channel}`;
});

operations.pub.forEach((metadata,publisher) => {
const service = publisher.replace(/\s/g,'');
plantUMLSyntax+=`\nserver${i} --|> ${service}:${channel}`;
});
});
i++;
});
return `@startuml\ntitle Classes - Class Diagram\n${plantUMLSyntax}\n@enduml`;
}

module.exports = getPlantUMLDiagram;

0 comments on commit 0abf06e

Please sign in to comment.