-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added cli support for decorators and vocab (#37)
* feat: added support for applying vocab and decorators to a model Signed-off-by: Muskan Bararia <[email protected]> * feat: added support for applying vocab and decorators to a model Signed-off-by: Muskan Bararia <[email protected]> * feat: Added support for output and standardised arg params Signed-off-by: Muskan Bararia <[email protected]> * feat: Added support for output and standardised arg params Signed-off-by: Muskan Bararia <[email protected]> * fix:Updated aliases for models Signed-off-by: Muskan Bararia <[email protected]> * fix: updated jsdoc for options Signed-off-by: Muskan Bararia <[email protected]> --------- Signed-off-by: Muskan Bararia <[email protected]> Co-authored-by: Muskan Bararia <[email protected]>
- Loading branch information
1 parent
cecce4d
commit 3258869
Showing
12 changed files
with
353 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
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 |
---|---|---|
|
@@ -36,7 +36,8 @@ const Concerto = require('@accordproject/concerto-core').Concerto; | |
const CodeGen = require('@accordproject/concerto-codegen').CodeGen; | ||
|
||
const { Compare, compareResultToString } = require('@accordproject/concerto-analysis'); | ||
const { ModelFile, ModelManager } = require('@accordproject/concerto-core'); | ||
const { ModelFile, ModelManager,DecoratorManager } = require('@accordproject/concerto-core'); | ||
const { VocabularyManager } = require('@accordproject/concerto-vocabulary'); | ||
|
||
/** | ||
* Utility class that implements the commands exposed by the CLI. | ||
|
@@ -628,6 +629,64 @@ class Commands { | |
inferredConcertoJsonModel.models[0] | ||
); | ||
} | ||
|
||
/** | ||
* Decorate a given model with given list of dcs and vocab files and print the result | ||
* @param {string} modelFiles - the model to which vocab and decorator has to be applied | ||
* @param {string[]} dcsFiles - the decorator files to be applied to model | ||
* @param {string[]} vocFiles - the vocab files to be applied to model | ||
* @param {object} options - optional parameters | ||
* @param {string} options.format - format of output models (CTO or JSON) | ||
* @param {string} options.output - the output directory where the decorated models are to be written | ||
* @returns {string} - Depending on the options, CTO string or JSON string | ||
*/ | ||
static async decorate(modelFiles, dcsFiles,vocFiles,options) { | ||
try { | ||
const allModelContent = modelFiles.map(file => fs.readFileSync(file, 'utf-8')); | ||
const allDCSFiles = dcsFiles ? dcsFiles.map(file => fs.readFileSync(file, 'utf-8')) : []; | ||
const allVocsFiles = vocFiles ? vocFiles.map(file => fs.readFileSync(file, 'utf-8')) : []; | ||
let modelManager = new ModelManager(); | ||
allModelContent.forEach(modelContent => { | ||
modelManager.addModel(modelContent); | ||
}); | ||
allDCSFiles.forEach(content => { | ||
modelManager = DecoratorManager.decorateModels(modelManager, JSON.parse(content)); | ||
}); | ||
const vocManager = new VocabularyManager({ missingTermGenerator: VocabularyManager.englishMissingTermGenerator }); | ||
const namespace = modelManager.getNamespaces().filter(namespace=>namespace!=='[email protected]' && namespace!=='concerto'); | ||
allVocsFiles.forEach(content => { | ||
vocManager.addVocabulary(content); | ||
}); | ||
const vocabKeySet=[]; | ||
namespace.forEach(name=>{ | ||
let vocab = vocManager.getVocabulariesForNamespace(name); | ||
vocab.forEach(voc=>vocabKeySet.push(voc.getLocale())); | ||
}); | ||
vocabKeySet.map(voc=>{ | ||
let commandSet = vocManager.generateDecoratorCommands(modelManager, voc); | ||
modelManager = DecoratorManager.decorateModels(modelManager, commandSet); | ||
}); | ||
let result=[]; | ||
const extension = (options.format === 'cto') ? '.cto':'.json'; | ||
namespace.forEach(name=>{ | ||
let model = modelManager.getModelFile(name); | ||
let modelAst=model.getAst(); | ||
let data = (options.format === 'cto') ? Printer.toCTO(modelAst):JSON.stringify(modelAst); | ||
if (options.output) { | ||
if (!fs.existsSync(options.output)) { | ||
// If it doesn't exist, create the directory | ||
fs.mkdirSync(options.output); | ||
} | ||
const filePath = path.join(options.output, model.namespace.split('@')[0]+extension); | ||
fs.writeFileSync(filePath, data); | ||
} | ||
result.push(data); | ||
}); | ||
return result; | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Commands; |
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,51 @@ | ||
{ | ||
"$class": "org.accordproject.decoratorcommands.DecoratorCommandSet", | ||
"name": "pii", | ||
"version": "1.0.0", | ||
"commands": [ | ||
{ | ||
"$class": "org.accordproject.decoratorcommands.Command", | ||
"type": "UPSERT", | ||
"target": { | ||
"$class": "org.accordproject.decoratorcommands.CommandTarget", | ||
"property": "ssn" | ||
}, | ||
"decorator": { | ||
"$class": "[email protected]", | ||
"name": "PII", | ||
"arguments": [ | ||
] | ||
} | ||
}, | ||
{ | ||
"$class": "org.accordproject.decoratorcommands.Command", | ||
"type": "UPSERT", | ||
"target": { | ||
"$class": "org.accordproject.decoratorcommands.CommandTarget", | ||
"property": "bar" | ||
}, | ||
"decorator": { | ||
"$class": "[email protected]", | ||
"name": "PII", | ||
"arguments": [ | ||
] | ||
} | ||
}, | ||
{ | ||
"$class": "org.accordproject.decoratorcommands.Command", | ||
"type": "UPSERT", | ||
"target": { | ||
"$class": "org.accordproject.decoratorcommands.CommandTarget", | ||
"type": "[email protected]" | ||
}, | ||
"decorator": { | ||
"$class": "[email protected]", | ||
"name": "Hide", | ||
"arguments": [{ | ||
"$class" : "[email protected]", | ||
"value" : "object" | ||
}] | ||
} | ||
} | ||
] | ||
} |
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,6 @@ | ||
locale: en-gb | ||
namespace: [email protected] | ||
declarations: | ||
- Driver: A driver of a vehicle | ||
properties: | ||
- favoriteColor: favourite colour |
Empty file.
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,23 @@ | ||
namespace [email protected] | ||
|
||
abstract concept Person identified by ssn { | ||
o String firstName | ||
o String lastName | ||
@PII() | ||
o String ssn | ||
} | ||
|
||
concept Driver extends Person { | ||
o String favoriteColor | ||
} | ||
|
||
concept Employee { | ||
@PII() | ||
o String ssn | ||
} | ||
|
||
concept Car identified by vin { | ||
o String vin | ||
@Hide("object") | ||
o Person owner | ||
} |
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,6 @@ | ||
namespace [email protected] | ||
|
||
concept Boo { | ||
@PII() | ||
o String bar | ||
} |
Oops, something went wrong.