Skip to content

Commit

Permalink
feat(simulator): support sub-package resource definitions (#332)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jianzs authored Aug 22, 2024
1 parent 50d6df6 commit 75e5a89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/new-items-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@plutolang/simulator-adapter": patch
---

feat(simulator): support sub-package resource definitions

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.
22 changes: 15 additions & 7 deletions components/adapters/simulator/src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,24 @@ export class Simulator {

private async createResource(resource: arch.Resource): Promise<simulator.IResourceInstance> {
const resourceTypeFqn = resource.type;
const dotPos = resourceTypeFqn.lastIndexOf(".");

const dotPos = resourceTypeFqn.indexOf(".");
const pkgName = resourceTypeFqn.substring(0, dotPos);
const resourceType = resourceTypeFqn.substring(dotPos + 1);
// TODO: check if the package exists, and import from user project

const infraPkg = (await import(resolvePkg(`${pkgName}-infra`))) as any;
const resourceInfraClass = infraPkg[resourceType];
if (!resourceInfraClass) {
throw new Error(
"Cannot find the infrastructure implementation class of the resource type " + resourceType
);
const importLevels = resourceType.split(".");

let resourceInfraClass: any;
for (let i = 0; i < importLevels.length; i++) {
resourceInfraClass = i == 0 ? infraPkg[importLevels[i]] : resourceInfraClass[importLevels[i]];

if (!resourceInfraClass) {
throw new Error(
"Cannot find the infrastructure implementation class of the resource type " +
resourceTypeFqn
);
}
}

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

0 comments on commit 75e5a89

Please sign in to comment.