Skip to content

Commit

Permalink
fix(codegen) fix code generation to support versioned namespaces (#521)
Browse files Browse the repository at this point in the history
* fix(codegen) fix code generation to support versioned namespaces

Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman authored Sep 26, 2022
1 parent db932f6 commit c43b1a4
Show file tree
Hide file tree
Showing 35 changed files with 4,101 additions and 7,583 deletions.
332 changes: 177 additions & 155 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dayjs": "1.10.8",
"eslint": "8.2.0",
"jsdoc": "^3.6.7",
"glob": "^7.1.7",
"glob": "^7.2.0",
"lerna": "^4.0.0",
"license-check-and-add": "2.3.6",
"nyc": "15.1.0",
Expand Down
51 changes: 6 additions & 45 deletions packages/concerto-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ const CodeGen = require('@accordproject/concerto-tools').CodeGen;
const { Compare, compareResultToString } = require('@accordproject/concerto-analysis');
const { ModelFile, ModelManager } = require('@accordproject/concerto-core');

const GoLangVisitor = CodeGen.GoLangVisitor;
const JavaVisitor = CodeGen.JavaVisitor;
const JSONSchemaVisitor = CodeGen.JSONSchemaVisitor;
const PlantUMLVisitor = CodeGen.PlantUMLVisitor;
const TypescriptVisitor = CodeGen.TypescriptVisitor;
const XmlSchemaVisitor = CodeGen.XmlSchemaVisitor;
const GraphQLVisitor = CodeGen.GraphQLVisitor;
const CSharpVisitor = CodeGen.CSharpVisitor;
const ODataVisitor = CodeGen.ODataVisitor;

/**
* Utility class that implements the commands exposed by the CLI.
* @class
Expand Down Expand Up @@ -163,45 +153,16 @@ class Commands {
modelManager.addCTOModel(MetaModelUtil.metaModelCto);
}

let visitor = null;

switch(target.toLowerCase()) {
case 'go':
visitor = new GoLangVisitor();
break;
case 'plantuml':
visitor = new PlantUMLVisitor();
break;
case 'typescript':
visitor = new TypescriptVisitor();
break;
case 'java':
visitor = new JavaVisitor();
break;
case 'jsonschema':
visitor = new JSONSchemaVisitor();
break;
case 'xmlschema':
visitor = new XmlSchemaVisitor();
break;
case 'graphql':
visitor = new GraphQLVisitor();
break;
case 'csharp':
visitor = new CSharpVisitor();
break;
case 'odata':
visitor = new ODataVisitor();
break;
}

if(visitor) {
const visitorClass = CodeGen.formats[target.toLowerCase()];
if(visitorClass) {
const visitor = new visitorClass;
let parameters = visitorOptions;
parameters.fileWriter = new FileWriter(output);
modelManager.accept(visitor, parameters);
return `Compiled to ${target} in '${output}'.`;
} else {
return 'Unrecognized target: ' + target;
}
else {
return `Unrecognized target: ${target}. Supported targets are: ${Object.keys(CodeGen.formats)}`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/concerto-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"eslint": "8.2.0",
"jsdoc": "^3.6.7",
"license-check-and-add": "2.3.6",
"mocha": "8.3.2",
"mocha": "10.0.0",
"nyc": "15.1.0",
"sinon": "12.0.0",
"sinon-chai": "3.7.0",
Expand All @@ -50,7 +50,7 @@
"@accordproject/concerto-tools": "3.0.0-beta.1",
"@accordproject/concerto-util": "3.0.0-beta.1",
"ansi-colors": "4.1.3",
"glob": "7.2.0",
"glob": "^7.2.0",
"semver": "7.3.5",
"yargs": "17.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('concerto-cli', () => {

it('should compile to a Go model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true });
await Commands.compile('Go', models, dir.path, {offline:false});
await Commands.compile('GoLang', models, dir.path, {offline:false});
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/lib/introspect/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Field extends Property {

/**
* Returns the default value for the field or null
* @return {string} the default value for the field or null
* @return {string | number} the default value for the field or null
*/
getDefaultValue() {
if(this.defaultValue) {
Expand Down
16 changes: 16 additions & 0 deletions packages/concerto-core/lib/modelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ class ModelUtil {
return type;
}
}

/**
* Converts a fully qualified type name to a FQN without a namespace version.
* If the FQN is a primitive type it is returned unchanged.
* @param {string} fqn fully qualified name of a type
* @returns {string} the fully qualified name minus the namespace version
*/
static removeNamespaceVersionFromFullyQualifiedName(fqn) {
if(ModelUtil.isPrimitiveType(fqn)) {
return fqn;
}
const ns = ModelUtil.getNamespace(fqn);
const { name: namespace } = ModelUtil.parseNamespace(ns);
const typeName = ModelUtil.getShortName(fqn);
return ModelUtil.getFullyQualifiedName(namespace, typeName);
}
}

module.exports = ModelUtil;
2 changes: 1 addition & 1 deletion packages/concerto-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/concerto-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@
"exclude": [],
"all": true,
"check-coverage": true,
"statements": 98,
"branches": 97,
"functions": 97,
"lines": 98
"statements": 99,
"branches": 98,
"functions": 98,
"lines": 99
}
}
13 changes: 13 additions & 0 deletions packages/concerto-core/test/modelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ describe('ModelUtil', function () {

});

describe('#removeNamespaceVersionFromFullyQualifiedName', function() {
it('valid inputs', function() {
const result = ModelUtil.removeNamespaceVersionFromFullyQualifiedName('[email protected]');
result.should.equal('org.acme.Person');
});

it('primtive type', function() {
const result = ModelUtil.removeNamespaceVersionFromFullyQualifiedName('String');
result.should.equal('String');
});

});

describe('#parseNamespace', function() {
it('valid, no version', function() {
const nsInfo = ModelUtil.parseNamespace('org.acme');
Expand Down
4 changes: 2 additions & 2 deletions packages/concerto-core/types/lib/introspect/field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ declare class Field extends Property {
getValidator(): Validator;
/**
* Returns the default value for the field or null
* @return {string} the default value for the field or null
* @return {string | number} the default value for the field or null
*/
getDefaultValue(): string;
getDefaultValue(): string | number;
/**
* Returns true if this class is the definition of a field.
*
Expand Down
7 changes: 7 additions & 0 deletions packages/concerto-core/types/lib/modelutil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ declare class ModelUtil {
* @returns {string} the fully qualified type name.
*/
static getFullyQualifiedName(namespace: string, type: string): string;
/**
* Converts a fully qualified type name to a FQN without a namespace version.
* If the FQN is a primitive type it is returned unchanged.
* @param {string} fqn fully qualified name of a type
* @returns {string} the fully qualified name minus the namespace version
*/
static removeNamespaceVersionFromFullyQualifiedName(fqn: string): string;
}
Loading

0 comments on commit c43b1a4

Please sign in to comment.