Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(simulator): support sub-package resource definitions #332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading