Skip to content

Commit

Permalink
fix(resolve): resolve namespace for decorators on primitives (#25)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman authored Sep 24, 2024
1 parent 2ef8348 commit 229b6f4
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 30 deletions.
24 changes: 24 additions & 0 deletions categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$class": "[email protected]",
"decorators": [],
"namespace": "[email protected]",
"imports": [],
"declarations": [
{
"$class": "[email protected]",
"name": "Category",
"isAbstract": true,
"properties": []
},
{
"$class": "[email protected]",
"name": "Engineering",
"isAbstract": false,
"properties": [],
"superType": {
"$class": "[email protected]",
"name": "Category"
}
}
]
}
79 changes: 79 additions & 0 deletions decorators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"$class": "[email protected]",
"decorators": [],
"namespace": "[email protected]",
"imports": [
{
"$class": "[email protected]",
"namespace": "[email protected]",
"types": [
"Engineering"
]
}
],
"declarations": [
{
"$class": "[email protected]",
"name": "Car",
"isAbstract": false,
"properties": [
{
"$class": "[email protected]",
"name": "part",
"type": {
"$class": "[email protected]",
"name": "Part"
},
"isArray": false,
"isOptional": false
}
]
},
{
"$class": "[email protected]",
"name": "Part",
"isAbstract": true,
"properties": [
{
"$class": "[email protected]",
"name": "name",
"isArray": false,
"isOptional": false,
"decorators": [
{
"$class": "[email protected]",
"name": "category",
"arguments": [
{
"$class": "[email protected]",
"type": {
"$class": "[email protected]",
"name": "Engineering"
},
"isArray": false
}
]
}
]
}
]
},
{
"$class": "[email protected]",
"name": "Wheel",
"isAbstract": false,
"properties": [
{
"$class": "[email protected]",
"name": "diameter",
"isArray": false,
"isOptional": false
}
],
"superType": {
"$class": "[email protected]",
"name": "Part"
}
}
]
}
39 changes: 9 additions & 30 deletions lib/metamodelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ function resolveName(name, table) {
* @return {object} the metamodel with fully qualified names
*/
function resolveTypeNames(metaModel, table) {
// any element can have a decorator (including primitive fields) , so lets resolve those first
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});

switch (metaModel.$class) {
case `${MetaModelNamespace}.Model`: {
(metaModel.declarations || []).forEach((decl) => {
Expand All @@ -145,49 +150,26 @@ function resolveTypeNames(metaModel, table) {
(metaModel.properties || []).forEach((property) => {
resolveTypeNames(property, table);
});
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});
}
break;
case `${MetaModelNamespace}.EnumDeclaration`: {
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});
}
break;
case `${MetaModelNamespace}.MapDeclaration`: {
resolveTypeNames(metaModel.key, table);
resolveTypeNames(metaModel.value, table);
}
break;
case `${MetaModelNamespace}.EnumProperty`:
case `${MetaModelNamespace}.ObjectProperty`:
case `${MetaModelNamespace}.RelationshipProperty`: {
const name = metaModel.type.name;
metaModel.type.namespace = resolveName(name, table);
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});
}
break;
case `${MetaModelNamespace}.Decorator`: {
(metaModel.arguments || []).forEach((argument) => {
resolveTypeNames(argument, table);
});
}
break;
case `${MetaModelNamespace}.DecoratorTypeReference`: {
const name = metaModel.type.name;
metaModel.type.namespace = resolveName(name, table);
}
break;
case `${MetaModelNamespace}.EnumProperty`:
case `${MetaModelNamespace}.ObjectProperty`:
case `${MetaModelNamespace}.RelationshipProperty`:
case `${MetaModelNamespace}.DecoratorTypeReference`:
case `${MetaModelNamespace}.ObjectMapKeyType`:
case `${MetaModelNamespace}.ObjectMapValueType`: {
metaModel.type.namespace = resolveName(metaModel.type.name, table);
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});
}
break;
case `${MetaModelNamespace}.StringScalar`:
Expand All @@ -197,9 +179,6 @@ function resolveTypeNames(metaModel, table) {
case `${MetaModelNamespace}.LongScalar`:
case `${MetaModelNamespace}.IntegerScalar`: {
metaModel.namespace = resolveName(metaModel.name, table);
(metaModel.decorators || []).forEach((decorator) => {
resolveTypeNames(decorator, table);
});
}
break;
}
Expand Down
4 changes: 4 additions & 0 deletions test/cto/categories.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace [email protected]

abstract concept Category {}
concept Engineering extends Category {}
16 changes: 16 additions & 0 deletions test/cto/decorators.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace [email protected]

import [email protected].{Engineering}

concept Car {
o Part part
}

abstract concept Part {
@category(Engineering)
o String name
}

concept Wheel extends Part {
o Double diameter
}
108 changes: 108 additions & 0 deletions test/cto/decorators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"$class": "[email protected]",
"models": [
{
"$class": "[email protected]",
"decorators": [],
"namespace": "[email protected]",
"imports": [],
"declarations": [
{
"$class": "[email protected]",
"name": "Category",
"isAbstract": true,
"properties": []
},
{
"$class": "[email protected]",
"name": "Engineering",
"isAbstract": false,
"properties": [],
"superType": {
"$class": "[email protected]",
"name": "Category"
}
}
]
},
{
"$class": "[email protected]",
"decorators": [],
"namespace": "[email protected]",
"imports": [
{
"$class": "[email protected]",
"namespace": "[email protected]",
"types": [
"Engineering"
]
}
],
"declarations": [
{
"$class": "[email protected]",
"name": "Car",
"isAbstract": false,
"properties": [
{
"$class": "[email protected]",
"name": "part",
"type": {
"$class": "[email protected]",
"name": "Part"
},
"isArray": false,
"isOptional": false
}
]
},
{
"$class": "[email protected]",
"name": "Part",
"isAbstract": true,
"properties": [
{
"$class": "[email protected]",
"name": "name",
"isArray": false,
"isOptional": false,
"decorators": [
{
"$class": "[email protected]",
"name": "category",
"arguments": [
{
"$class": "[email protected]",
"type": {
"$class": "[email protected]",
"name": "Engineering"
},
"isArray": false
}
]
}
]
}
]
},
{
"$class": "[email protected]",
"name": "Wheel",
"isAbstract": false,
"properties": [
{
"$class": "[email protected]",
"name": "diameter",
"isArray": false,
"isOptional": false
}
],
"superType": {
"$class": "[email protected]",
"name": "Part"
}
}
]
}
]
}
Loading

0 comments on commit 229b6f4

Please sign in to comment.