Skip to content

Commit

Permalink
feat(vocabulary): resolved review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sanket Shevkar <sanket.shevkar@docusign.com>
  • Loading branch information
Sanket Shevkar committed Nov 7, 2023
1 parent 32dbe87 commit 5a9742b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
26 changes: 15 additions & 11 deletions packages/concerto-vocabulary/lib/vocabulary.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,43 +144,47 @@ class Vocabulary {
* @returns {*} an object with missingTerms and additionalTerms properties
*/
validate(modelFile) {
const getOwnProperties = (d) => {
const getOwnProperties = (declaration) => {
// ensures we have a valid return, even for scalars and map-declarations
return d.isMapDeclaration() ? [d.getKey(), d.getValue()] : d.getOwnProperties?.() ? d.getOwnProperties?.() : [];
if(declaration.isMapDeclaration()) {
return [declaration.getKey(), declaration.getValue()];
} else {
return declaration.getOwnProperties?.() ? declaration.getOwnProperties?.() : [];
}
};

const getPropertyName = (p) => {
if(p.isKey?.()) {
const getPropertyName = (property) => {
if(property.isKey?.()) {
return 'KEY';
} else if(p.isValue?.()) {
} else if(property.isValue?.()) {
return 'VALUE';
} else {
return p.getName();
return property.getName();
}
};

const checkProperties = (k, p) => {
const checkPropertyExists = (k, p) => {
const declaration = modelFile.getLocalType(Object.keys(k)[0]);
const property = Object.keys(p)[0];
if(declaration.isMapDeclaration()) {
if (!property.localeCompare('KEY')) {
if (property === 'KEY') {
return true;
} else if(!property.localeCompare('VALUE')) {
} else if(property === 'VALUE') {
return true;
} else {
return false;
}
} else {
return declaration.getOwnProperty(Object.keys(p)[0]);
}

};

const result = {
missingTerms: modelFile.getAllDeclarations().flatMap( d => this.getTerm(d.getName())
? getOwnProperties(d).flatMap( p => this.getTerm(d.getName(), getPropertyName(p)) ? null : `${d.getName()}.${getPropertyName(p)}`)
: d.getName() ).filter( i => i !== null),
additionalTerms: this.content.declarations.flatMap( k => modelFile.getLocalType(Object.keys(k)[0])
? Array.isArray(k.properties) ? k.properties.flatMap( p => checkProperties(k, p) ? null : `${Object.keys(k)[0]}.${Object.keys(p)[0]}`) : null
? Array.isArray(k.properties) ? k.properties.flatMap( p => checkPropertyExists(k, p) ? null : `${Object.keys(k)[0]}.${Object.keys(p)[0]}`) : null
: k ).filter( i => i !== null)
};

Expand Down
14 changes: 12 additions & 2 deletions packages/concerto-vocabulary/lib/vocabularymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ class VocabularyManager {
'commands': []
};

const getPropertyNames = (declaration) => {
if (declaration.getProperties) {
return declaration.getProperties().map(property => property.getName());
} else if(declaration.isMapDeclaration?.()) {
return ['KEY', 'VALUE'];
} else {
return [];
}
};

modelManager.getModelFiles().forEach(model => {
model.getAllDeclarations().forEach(decl => {
const terms = this.resolveTerms(modelManager, model.getNamespace(), locale, decl.getName());
Expand Down Expand Up @@ -347,12 +357,12 @@ class VocabularyManager {
});
}

const propertyNames = decl.getProperties ? decl.getProperties().map(property => property.getName()) : decl.isMapDeclaration ? decl.isMapDeclaration() ? ['KEY', 'VALUE'] : [] : [];
const propertyNames = getPropertyNames(decl);
propertyNames.forEach(propertyName => {
const propertyTerms = this.resolveTerms(modelManager, model.getNamespace(), locale, decl.getName(), propertyName);
if (propertyTerms) {
Object.keys(propertyTerms).forEach( term => {
const propertyType = !propertyName.localeCompare('KEY') || !propertyName.localeCompare('VALUE') ? 'mapElement' : 'property';
const propertyType = propertyName === 'KEY' || propertyName === 'VALUE' ? 'mapElement' : 'property';
if(term === propertyName) {
decoratorCommandSet.commands.push({
'$class': `${DC_NAMESPACE}.Command`,
Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-vocabulary/test/vocabularymanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let vocabularyManager = null;

describe('VocabularyManager', () => {
beforeEach(() => {
modelManager = new ModelManager({ enableMapType: true});
process.env.ENABLE_MAP_TYPE = 'true'; // TODO Remove on release of MapType
modelManager = new ModelManager();
const model = fs.readFileSync('./test/org.acme@1.0.0.cto', 'utf-8');
modelManager.addCTOModel(model);
const model2 = fs.readFileSync('./test/org.accordproject@1.0.0.cto', 'utf-8');
Expand Down

0 comments on commit 5a9742b

Please sign in to comment.