Skip to content

Commit

Permalink
feat(dcs): refactor to use enum for target elements
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Casey <jonathan.casey@docusign.com>
  • Loading branch information
jonathan-casey committed Oct 4, 2023
1 parent d51e5ef commit 90ebf47
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
30 changes: 17 additions & 13 deletions packages/concerto-core/lib/decoratormanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ concept CommandTarget {
o String property optional
o String[] properties optional // property and properties are mutually exclusive
o String type optional
o String mapElement optional
o MapElement mapElement optional
}
/**
* Map Declaration elements which might be used as a target
*/
enum MapElement {
o KEY
o VALUE
o KEY_VALUE
}
/**
Expand Down Expand Up @@ -249,7 +258,7 @@ class DecoratorManager {
* @param {*} newDecorator the decorator to add
*/
static applyDecoratorForMapElement(element, target, declaration, type, newDecorator ) {
const decl = element.toLowerCase() === 'key' ? declaration.key : declaration.value;
const decl = element === 'KEY' ? declaration.key : declaration.value;
if (target.type) {
if (this.falsyOrEqual(target.type, decl.$class)) {
this.applyDecorator(decl, type, newDecorator);
Expand Down Expand Up @@ -324,20 +333,15 @@ class DecoratorManager {

if (declaration.$class === 'concerto.metamodel@1.0.0.MapDeclaration') {
if (target.mapElement) {
switch(target.mapElement.toLowerCase()) {
case 'key':
switch(target.mapElement) {
case 'KEY':
case 'VALUE':
this.applyDecoratorForMapElement(target.mapElement, target, declaration, type, decorator);
break;
case 'value':
this.applyDecoratorForMapElement(target.mapElement, target, declaration, type, decorator);
break;
case '*':
this.applyDecoratorForMapElement('key', target, declaration, type, decorator);
this.applyDecoratorForMapElement('value', target, declaration, type, decorator);

case 'KEY_VALUE':
this.applyDecoratorForMapElement('KEY', target, declaration, type, decorator);
this.applyDecoratorForMapElement('VALUE', target, declaration, type, decorator);
break;
default:
throw new Error('Decorator Command contains invalid target for Map element: ' + target.mapElement );
}
} else if (target.type) {
if (this.falsyOrEqual(target.type, declaration.key.$class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"$class" : "org.accordproject.decoratorcommands@0.3.0.CommandTarget",
"namespace" : "test@1.0.0",
"declaration" : "Dictionary",
"mapElement": "*"
"mapElement": "KEY_VALUE"
},
"decorator" : {
"$class" : "concerto.metamodel@1.0.0.Decorator",
Expand All @@ -104,7 +104,7 @@
"$class" : "org.accordproject.decoratorcommands@0.3.0.CommandTarget",
"namespace" : "test@1.0.0",
"type" : "concerto.metamodel@1.0.0.StringMapKeyType",
"mapElement": "*"
"mapElement": "KEY_VALUE"
},
"decorator" : {
"$class" : "concerto.metamodel@1.0.0.Decorator",
Expand All @@ -119,7 +119,7 @@
"$class" : "org.accordproject.decoratorcommands@0.3.0.CommandTarget",
"namespace" : "test@1.0.0",
"type" : "concerto.metamodel@1.0.0.StringMapValueType",
"mapElement": "*"
"mapElement": "KEY_VALUE"
},
"decorator" : {
"$class" : "concerto.metamodel@1.0.0.Decorator",
Expand Down
14 changes: 0 additions & 14 deletions packages/concerto-core/test/decoratormanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,6 @@ describe('DecoratorManager', () => {
{validate: true, validateCommands: true});
}).should.throw(/Decorator Command references both property and properties. You must either reference a single property or a list of properites./);
});

it('should detect invalid target element', async function() {
// load a model to decorate
const testModelManager = new ModelManager({strict:true});
const modelText = fs.readFileSync('./test/data/decoratorcommands/test.cto', 'utf-8');
testModelManager.addCTOModel(modelText, 'test.cto');

const dcs = fs.readFileSync('./test/data/decoratorcommands/invalid-target-element.json', 'utf-8');

(() => {
DecoratorManager.decorateModels( testModelManager, JSON.parse(dcs),
{validate: true, validateCommands: true});
}).should.throw(/Decorator Command contains invalid target for Map element: INVALID_ELEMENT/);
});
});

describe('#validate', function() {
Expand Down

0 comments on commit 90ebf47

Please sign in to comment.