Skip to content

Commit

Permalink
feat(codegen): code generator to bootstrap vocab file (#21) (#22)
Browse files Browse the repository at this point in the history
* feat(codegen): code generator to bootstrap vocab file (#21)

Signed-off-by: sanketshevkar <[email protected]>

* chore(*): remove merge conflicts

Signed-off-by: sanketshevkar <[email protected]>

* refactor(codegen): visitor for scalar and vocab to vocabulary

Signed-off-by: sanketshevkar <[email protected]>

* refactor(codegen): visitor for scalar and vocab to vocabulary

Signed-off-by: sanketshevkar <[email protected]>

* chore(*): tests and refactoring

Signed-off-by: sanketshevkar <[email protected]>

* chore(package.json): update version for concerto-vocabulary

Signed-off-by: sanketshevkar <[email protected]>

---------

Signed-off-by: sanketshevkar <[email protected]>
  • Loading branch information
sanketshevkar authored Jun 8, 2023
1 parent 1041587 commit d38ddbc
Show file tree
Hide file tree
Showing 9 changed files with 968 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/codegen/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const JSONSchemaToConcertoVisitor = require(
);
const OpenApiToConcertoVisitor = require('./fromOpenApi/cto/openApiVisitor');
const RustVisitor = require('./fromcto/rust/rustvisitor');
const VocabularyVisitor = require('./fromcto/vocabulary/vocabularyvisitor');

module.exports = {
AbstractPlugin,
Expand All @@ -55,6 +56,7 @@ module.exports = {
JSONSchemaToConcertoVisitor,
OpenApiToConcertoVisitor,
RustVisitor,
VocabularyVisitor,
formats: {
golang: GoLangVisitor,
jsonschema: JSONSchemaVisitor,
Expand All @@ -70,6 +72,7 @@ module.exports = {
protobuf: ProtobufVisitor,
openapi: OpenApiVisitor,
avro: AvroVisitor,
rust: RustVisitor
rust: RustVisitor,
vocabulary: VocabularyVisitor
}
};
160 changes: 160 additions & 0 deletions lib/codegen/fromcto/vocabulary/vocabularyvisitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const VocabularyManager = require('@accordproject/concerto-vocabulary').VocabularyManager;
const util = require('util');

/**
* Convert the contents of a ModelManager to Vocabulary YAML.
* All generated code is placed into the 'main' package. Set a
* fileWriter property (instance of FileWriter) on the parameters
* object to control where the generated code is written to disk.
*
* @private
* @class
* @memberof module:concerto-codegen
*/
class VocabularyVisitor {
/**
* Visitor design pattern
* @param {Object} thing - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @public
*/
visit(thing, parameters) {
if (thing.isModelManager?.()) {
return this.visitModelManager(thing, parameters);
} else if (thing.isModelFile?.()) {
return this.visitModelFile(thing, parameters);
} else if (thing.isEnum?.()) {
return this.visitDeclaration(thing, parameters);
} else if (thing.isClassDeclaration?.()) {
return this.visitDeclaration(thing, parameters);
} else if (thing.isScalarDeclaration?.()) {
return this.visitScalarDeclaration(thing, parameters);
} else if (thing.isField?.()) {
return this.visitProperty(thing, parameters);
} else if (thing.isRelationship?.()) {
return this.visitProperty(thing, parameters);
} else if (thing.isEnumValue?.()) {
return this.visitProperty(thing, parameters);
}
else {
throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, {
showHidden: true,
depth: 2
}));
}
}

/**
* Visitor design pattern
* @param {ModelManager} modelManager - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitModelManager(modelManager, parameters) {
modelManager.getModelFiles().forEach((modelFile) => {
modelFile.accept(this, parameters);
});
return null;
}

/**
* Visitor design pattern
* @param {ModelFile} modelFile - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitModelFile(modelFile, parameters) {
parameters.fileWriter.openFile(modelFile.getNamespace() + '_en.voc');
parameters.fileWriter.writeLine(0, `#Generated vocabulary for namespace: ${modelFile.getNamespace()}`);
parameters.fileWriter.writeLine(0, 'locale: en');
parameters.fileWriter.writeLine(0, `namespace: ${modelFile.getNamespace()}`);
parameters.fileWriter.writeLine(0, 'declarations:');

modelFile.getAllDeclarations()
.forEach((decl) => {
decl.accept(this, parameters);
});

parameters.fileWriter.closeFile();

return null;
}

/**
* Visitor design pattern
* @param {Declaration} declaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitDeclaration(declaration, parameters) {
const declarationName = declaration.getName();
const declarationVocabulary = VocabularyManager.englishMissingTermGenerator(null, null, declarationName);

parameters.fileWriter.writeLine(0, ` - ${declarationName}: ${declarationVocabulary}`);


if (declaration.getOwnProperties().length) {
parameters.fileWriter.writeLine(0, ' properties:');
}
declaration.getOwnProperties().forEach((property) => {
property.accept(this, parameters);
});

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) {
const scalarDeclarationName = scalarDeclaration.getName();
const scalarDeclarationNameVocabulary = VocabularyManager.englishMissingTermGenerator(null, null, scalarDeclarationName);

parameters.fileWriter.writeLine(0, ` - ${scalarDeclarationName}: ${scalarDeclarationNameVocabulary}`);

return null;
}

/**
* Visitor design pattern
* @param {Property} property - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitProperty(property, parameters) {
const declarationName = property.getParent().getName();
const propertyName = property.getName();
const propertyVocabulary = VocabularyManager.englishMissingTermGenerator(null, null, declarationName, propertyName);

parameters.fileWriter.writeLine(0, ` - ${propertyName}: ${propertyVocabulary}`);

return null;
}
}

module.exports = VocabularyVisitor;
36 changes: 36 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"dependencies": {
"@accordproject/concerto-core": "3.8.1",
"@accordproject/concerto-util": "3.8.0",
"@accordproject/concerto-vocabulary": "3.8.2",
"@openapi-contrib/openapi-schema-to-json-schema": "3.2.0",
"ajv": "8.10.0",
"ajv-formats": "2.1.1",
Expand Down
Loading

0 comments on commit d38ddbc

Please sign in to comment.