Skip to content

Commit 75e5a89

Browse files
authored
feat(simulator): support sub-package resource definitions (#332)
Allow resource types to be defined within sub-packages, extending the previous limitation of only allowing definitions in the root package. This enhancement enables the simulator to process resources that are defined in sub-packages.
1 parent 50d6df6 commit 75e5a89

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

.changeset/new-items-stare.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@plutolang/simulator-adapter": patch
3+
---
4+
5+
feat(simulator): support sub-package resource definitions
6+
7+
Allow resource types to be defined within sub-packages, extending the previous limitation of only allowing definitions in the root package. This enhancement enables the simulator to process resources that are defined in sub-packages.

components/adapters/simulator/src/simulator.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,24 @@ export class Simulator {
7878

7979
private async createResource(resource: arch.Resource): Promise<simulator.IResourceInstance> {
8080
const resourceTypeFqn = resource.type;
81-
const dotPos = resourceTypeFqn.lastIndexOf(".");
81+
82+
const dotPos = resourceTypeFqn.indexOf(".");
8283
const pkgName = resourceTypeFqn.substring(0, dotPos);
8384
const resourceType = resourceTypeFqn.substring(dotPos + 1);
84-
// TODO: check if the package exists, and import from user project
85+
8586
const infraPkg = (await import(resolvePkg(`${pkgName}-infra`))) as any;
86-
const resourceInfraClass = infraPkg[resourceType];
87-
if (!resourceInfraClass) {
88-
throw new Error(
89-
"Cannot find the infrastructure implementation class of the resource type " + resourceType
90-
);
87+
const importLevels = resourceType.split(".");
88+
89+
let resourceInfraClass: any;
90+
for (let i = 0; i < importLevels.length; i++) {
91+
resourceInfraClass = i == 0 ? infraPkg[importLevels[i]] : resourceInfraClass[importLevels[i]];
92+
93+
if (!resourceInfraClass) {
94+
throw new Error(
95+
"Cannot find the infrastructure implementation class of the resource type " +
96+
resourceTypeFqn
97+
);
98+
}
9199
}
92100

93101
const args = new Array(resource.arguments.length);

0 commit comments

Comments
 (0)