From d496564bf94f698995f0f9f05b8dd370c3de57c4 Mon Sep 17 00:00:00 2001 From: Jade Zheng Date: Thu, 22 Aug 2024 12:22:17 +0800 Subject: [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. --- .changeset/new-items-stare.md | 7 ++++++ .../adapters/simulator/src/simulator.ts | 22 +++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 .changeset/new-items-stare.md diff --git a/.changeset/new-items-stare.md b/.changeset/new-items-stare.md new file mode 100644 index 00000000..c6b9109e --- /dev/null +++ b/.changeset/new-items-stare.md @@ -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. diff --git a/components/adapters/simulator/src/simulator.ts b/components/adapters/simulator/src/simulator.ts index 6cbaf38d..0c480c90 100644 --- a/components/adapters/simulator/src/simulator.ts +++ b/components/adapters/simulator/src/simulator.ts @@ -78,16 +78,24 @@ export class Simulator { private async createResource(resource: arch.Resource): Promise { 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);