Skip to content

Commit

Permalink
feat: alias Import rules added (#855)
Browse files Browse the repository at this point in the history
* feat: Alias Import rules added

* Rules for AliasedIdentifier and Types added.
* aliasedTypes filtered in callback of ImportTypes

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat: Grammar rules updated

- as token defined for aliasing types in the import statements
- as is defined local reserved word
- AST made backward compatible

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat: Printer updated

- Printer now handles the aliased types.
- Test case added for parser and printer.

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): Printer and parser test cases

- Test case added containing alias import

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): test cases for updated grammar

- test cases added for both parser and printer
- bad test cases added as well for the parser

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): PR changes

- renamed the Types rule to IdentifierTypeExpression
- Indentation fixed
- Extra white spaces removed

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias): Minor fixes

- Code changes made on suggestion.

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias): disabled aliasing to primitive types

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias): handle pr comments

- Test cases added
- pr comments handled

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias): variables renamed and code cleaned

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): printer code refactored

- printer code refactored
- coverage 100% tested locally

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): Updated concerto-metamodel version

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias import): Pr suggestion : code refactor

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias):package-lock.json fixed

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

* feat(alias): fixing pr

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>

---------

Signed-off-by: Jaskeerat Singh Saluja <58400083+salujajaskeerat@users.noreply.github.com>
  • Loading branch information
salujajaskeerat authored Jul 8, 2024
1 parent 731b2f1 commit 032bd75
Show file tree
Hide file tree
Showing 8 changed files with 858 additions and 559 deletions.
1,211 changes: 661 additions & 550 deletions packages/concerto-cto/lib/parser.js

Large diffs are not rendered by default.

43 changes: 36 additions & 7 deletions packages/concerto-cto/lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
namespace: split.join('.')
};
}
function isPrimitiveType(typeName) {
const primitiveTypes = ['Boolean', 'String', 'DateTime', 'Double', 'Integer', 'Long'];
return (primitiveTypes.indexOf(typeName) >= 0);
}
}

Start
Expand Down Expand Up @@ -419,6 +423,7 @@ FalseToken = "false" !IdentifierPart
ImportToken = "import" !IdentifierPart
NullToken = "null" !IdentifierPart
TrueToken = "true" !IdentifierPart
AsToken = "as" !IdentifierPart

/* Skipped */

Expand Down Expand Up @@ -1711,22 +1716,46 @@ ImportType
}
ImportTypes
= ImportToken __ ns:QualifiedNamespaceDeclaration ".{" _ types:commaSeparatedIdentifiers _ "}" __ u:FromUri? {
= ImportToken __ ns:QualifiedNamespaceDeclaration ".{" _ types:commaSeparatedTypes _ "}" __ u:FromUri? {
const { aliasedTypes, typesNames } = types.reduce((acc, type) => {
if (type.$class === "concerto.metamodel@1.0.0.AliasedType") {
acc.aliasedTypes.push(type);
acc.typesNames.push(type.name);
} else {
acc.typesNames.push(type);
}
return acc;
}, { aliasedTypes: [], typesNames: [] });
const result = {
$class: "concerto.metamodel@1.0.0.ImportTypes",
namespace: ns,
types,
types:typesNames,
... aliasedTypes.length >0 && {aliasedTypes},
};
u && (result.uri = u);
return result;
}
commaSeparatedIdentifiers
= head:$Identifier _ tail:(',' _ @$Identifier _ )*
{
return [head, ...tail];
}
AliasedIdentifier
= name:$Identifier _ $AsToken _ aliasedName:$Identifier{
if(isPrimitiveType(aliasedName)){
throw new Error(`A type cannot be aliased to a Primitive type, here "${name}" is being aliased as "${aliasedName}".`);
}
return {
"$class":"concerto.metamodel@1.0.0.AliasedType",
name,
aliasedName
};
}
IdentifierTypeExpression
= AliasedIdentifier / $Identifier
commaSeparatedTypes
= head:IdentifierTypeExpression _ tail:(',' _ IdentifierTypeExpression)* {
return [head, ...tail.map(t => t[2])];
}
Import
= ImportTypes /
ImportAll /
Expand Down
20 changes: 18 additions & 2 deletions packages/concerto-cto/lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,25 @@ function toCTO(metaModel) {
case `${MetaModelNamespace}.ImportAllFrom`:
result += `\nimport ${imp.namespace}.*`;
break;
case `${MetaModelNamespace}.ImportTypes`:
result += `\nimport ${imp.namespace}.{${imp.types.join(',')}}`;
case `${MetaModelNamespace}.ImportTypes`: {
const aliasedTypes = imp.aliasedTypes
? new Map(
imp.aliasedTypes.map(({ name, aliasedName }) => [
name,
aliasedName,
])
)
: new Map();
const commaSeparatedTypesString = imp.types
.map((type) =>
aliasedTypes.has(type)
? `${type} as ${aliasedTypes.get(type)}`
: type
)
.join(',');
result += `\nimport ${imp.namespace}.{${commaSeparatedTypesString}}`;
break;
}
default:
throw new Error('Unrecognized import');
}
Expand Down
9 changes: 9 additions & 0 deletions packages/concerto-cto/test/cto/aliasedImport.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace test.aliasing

import org.accordproject.{doc1 as d1,doc2 as d2}
import org.accordproject.files.{file1,file2 as f2,file3}
import com.example.foo@1.0.4-X.y.z-01.{baz1 as bar1}
import com.example.foo@1.0.4.{baz2 as bar2} from https://com.example.co.uk/models/com.example.foo.cto
import com.example.foo@1.0.4.{tee,baz as bar,lorem,ipsum,dolor as freq}
import com.example.foo@1.0.4.{As as as,as as as}
import com.example.foo@1.0.4.{baz}
101 changes: 101 additions & 0 deletions packages/concerto-cto/test/cto/aliasedImport.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"$class": "concerto.metamodel@1.0.0.Model",
"decorators": [],
"namespace": "test.aliasing",
"imports": [
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "org.accordproject",
"types": ["doc1", "doc2"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "doc1",
"aliasedName": "d1"
},
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "doc2",
"aliasedName": "d2"
}
]
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "org.accordproject.files",
"types": ["file1", "file2", "file3"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "file2",
"aliasedName": "f2"
}
]
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "com.example.foo@1.0.4-X.y.z-01",
"types": ["baz1"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "baz1",
"aliasedName": "bar1"
}
]
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "com.example.foo@1.0.4",
"types": ["baz2"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "baz2",
"aliasedName": "bar2"
}
],
"uri": "https://com.example.co.uk/models/com.example.foo.cto"
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "com.example.foo@1.0.4",
"types": ["tee", "baz", "lorem", "ipsum", "dolor"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "baz",
"aliasedName": "bar"
},
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "dolor",
"aliasedName": "freq"
}
]
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "com.example.foo@1.0.4",
"types": ["As", "as"],
"aliasedTypes": [
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "As",
"aliasedName": "as"
},
{
"$class": "concerto.metamodel@1.0.0.AliasedType",
"name": "as",
"aliasedName": "as"
}
]
},
{
"$class": "concerto.metamodel@1.0.0.ImportTypes",
"namespace": "com.example.foo@1.0.4",
"types": ["baz"]
}
],
"declarations": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test.bad.alias.missing@1.0.0

import ns.{doc as}

4 changes: 4 additions & 0 deletions packages/concerto-cto/test/cto/bad/aliasImport.bad.single.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace test.aliasing@1.0.0

//Bad Import : without paranthesis
import com.example.foo@1.0.4.baz as bar
25 changes: 25 additions & 0 deletions packages/concerto-cto/test/parserMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ describe('parser', () => {
});
});

describe('alias-imports',()=>{
it('Should not parse bad import alias: No parenthesis',()=>{
const content = fs.readFileSync('./test/cto/bad/aliasImport.bad.single.cto','utf-8');
(()=>{
Parser.parse(content);
}).should.throw(/Expected .+ but/);
});
it('Should not parse bad import alias: alias missing',()=>{
const content = fs.readFileSync('./test/cto/bad/aliasImport.bad.alias-missing.cto','utf-8');
(()=>{
Parser.parse(content);
}).should.throw(/Expected .+ but/);
});
it('Should throw when type is alias to a pimitive type',()=>{
const model=`
namespace org.saluja
import org.ece.{doc as String}
`;
(() => {
Parser.parse(model);
}).should.throw(/cannot be aliased to a Primitive type/);
});
});

describe('self-extending', () => {
const declarationTypes = [
'asset',
Expand Down

0 comments on commit 032bd75

Please sign in to comment.