Skip to content

Commit 425fc18

Browse files
salujajaskeeratJaskeerat Singh Saluja
andauthored
feat(aliasing): getImportedType() in model Flle in aliasing (#890)
* feat(i889): added the api Signed-off-by: Jaskeerat Singh Saluja <[email protected]> * feat(i889): added unit test-cases Signed-off-by: Jaskeerat Singh Saluja <[email protected]> * feat(i889): api.txt and changelog updated Signed-off-by: Jaskeerat Singh Saluja <[email protected]> * feat(i889): converage updated Signed-off-by: Jaskeerat Singh Saluja <[email protected]> * feat(alias import): function renamed Signed-off-by: Jaskeerat Singh Saluja <[email protected]> * feat(api):changelog updated Signed-off-by: Jaskeerat Singh Saluja <[email protected]> --------- Signed-off-by: Jaskeerat Singh Saluja <[email protected]> Co-authored-by: Jaskeerat Singh Saluja <[email protected]>
1 parent 737a516 commit 425fc18

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

packages/concerto-core/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class ModelFile extends Decorated {
214214
+ ModelManager getModelManager()
215215
+ string[] getImports()
216216
~ void validate() throws IllegalModelException
217+
+ string getImportedType(string)
217218
+ boolean isDefined(string)
218219
+ ClassDeclaration getLocalType(string)
219220
+ AssetDeclaration getAssetDeclaration(string)

packages/concerto-core/changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
# Note that the latest public API is documented using JSDocs and is available in api.txt.
2525
#
2626

27+
Version 3.17.4 {8cb49c7092c48b568da3b0a3eeab2777} 2024-08-01
28+
- Added api getImportedType() in modeFile
29+
2730
Version 3.17.3 {1c15de121dd80375cf531bc2244efdd0} 2024-07-26
2831
- Added new methods to extract vocab or non-vocab decorators from model
2932
- Fixed some minor bugs related to vocabulary parsing

packages/concerto-core/lib/introspect/modelfile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ class ModelFile extends Decorated {
389389
}),this);
390390
}
391391

392+
/**
393+
* Returns the actual imported name from another namespace
394+
* @param {string} type - the short name of the type
395+
* @returns {string} - the actual imported name. If not aliased then returns the same string
396+
*/
397+
getImportedType(type) {
398+
let fqn = this.resolveImport(type);
399+
return fqn.split('.').pop();
400+
}
401+
392402
/**
393403
* Returns true if the type is defined in the model file
394404
* @param {string} type the name of the type

packages/concerto-core/test/introspect/modelfile.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ describe('ModelFile', () => {
680680
const model2 = `
681681
namespace org.acme
682682
import org.saluja.{Student as stud}
683-
683+
684684
map StudMap{
685685
o DateTime
686686
o stud
@@ -695,17 +695,17 @@ describe('ModelFile', () => {
695695
it('should not throw if declaration is extended on a aliased type declaration', () => {
696696
const model1 = `
697697
namespace org.saluja
698-
698+
699699
scalar nickname extends String
700700
asset Vehicle identified by serialno {
701701
o String serialno
702702
}`;
703703
const model2 = `
704704
namespace org.acme
705705
import org.saluja.{Vehicle as V,nickname as nk}
706-
706+
707707
asset Car extends V{
708-
o String company
708+
o String company
709709
o nk shortname
710710
}`;
711711
modelManager.enableMapType = true;
@@ -714,6 +714,30 @@ describe('ModelFile', () => {
714714
let modelFile2 = ParserUtil.newModelFile(modelManager, model2);
715715
(() => modelFile2.validate()).should.not.throw();
716716
});
717+
718+
it('should return the actual type name of the imported type', () => {
719+
const model1 = `
720+
namespace org.example1
721+
722+
scalar nickname extends String
723+
asset Vehicle identified by serialno {
724+
o String serialno
725+
}`;
726+
const model2 = `
727+
namespace org.example2
728+
import org.example1.{Vehicle as V,nickname}
729+
730+
asset Car extends V{
731+
o String company
732+
o nickname shortname
733+
}`;
734+
modelManager.enableMapType = true;
735+
let modelFile1 = ParserUtil.newModelFile(modelManager, model1);
736+
modelManager.addModelFile(modelFile1);
737+
let modelFile2 = ParserUtil.newModelFile(modelManager, model2);
738+
modelFile2.getImportedType('V').should.equal('Vehicle');
739+
modelFile2.getImportedType('nickname').should.equal('nickname');
740+
});
717741
});
718742

719743

packages/concerto-core/types/lib/introspect/modelfile.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ declare class ModelFile extends Decorated {
120120
* @private
121121
*/
122122
private resolveImport;
123+
/**
124+
* Returns the actual imported name from another namespace
125+
* @param {string} type - the short name of the type
126+
* @returns {string} - the actual imported name. If not aliased then returns the same string
127+
*/
128+
getImportedType(type: string): string;
123129
/**
124130
* Returns true if the type is defined in the model file
125131
* @param {string} type the name of the type

0 commit comments

Comments
 (0)