-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
1041587
commit d38ddbc
Showing
9 changed files
with
968 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.