From 1c13cc05a7d2492a45f00180b1316e1f7d608081 Mon Sep 17 00:00:00 2001 From: Zaid Ajaj Date: Sat, 11 Jan 2025 19:42:03 +0100 Subject: [PATCH] simplify invokes inside range expressions --- pkg/codegen/java/gen_program.go | 78 ++++++++----------- .../testdata/aws-eks-pp/java/aws-eks.java | 48 ++++++++---- .../aws-s3-folder-pp/java/aws-s3-folder.java | 2 +- .../java/simple-invoke-with-range.java | 25 +++--- 4 files changed, 80 insertions(+), 73 deletions(-) diff --git a/pkg/codegen/java/gen_program.go b/pkg/codegen/java/gen_program.go index 3b88f0152f0..6bbcedc5876 100644 --- a/pkg/codegen/java/gen_program.go +++ b/pkg/codegen/java/gen_program.go @@ -1160,56 +1160,42 @@ func (g *generator) genResource(w io.Writer, resource *pcl.Resource) { } else { // for each-loop through the elements to creates a resource from each one - switch rangeExpr.(type) { - case *model.FunctionCallExpression: - funcCall := rangeExpr.(*model.FunctionCallExpression) - switch funcCall.Name { - case pcl.IntrinsicConvert: - firstArg := funcCall.Args[0] - switch firstArg := firstArg.(type) { - case *model.ScopeTraversalExpression: - traversalExpr := firstArg - if len(traversalExpr.Parts) == 2 { - // Meaning here we have {root}.{part} expression which the most common - // check whether {root} is actually a variable name that holds the result - // of a function invoke - if functionSchema, isInvoke := g.functionInvokes[traversalExpr.RootName]; isInvoke { - resultTypeName := names.LowerCamelCase(typeName(functionSchema.Outputs)) - part := getTraversalKey(traversalExpr.Traversal.SimpleSplit().Rel) - g.genIndent(w) - g.Fgenf(w, "final var %s = ", resource.Name()) - g.Fgenf(w, "%s.applyValue(%s -> {\n", traversalExpr.RootName, resultTypeName) - g.Indented(func() { - g.Fgenf(w, "%sfinal var resources = new ArrayList<%s>();\n", g.Indent, resourceTypeName) - g.Fgenf(w, "%sfor (var range : KeyedValue.of(%s.%s()) {\n", g.Indent, resultTypeName, part) - g.Indented(func() { - suffix := "range.key()" - g.Fgenf(w, "%svar resource = ", g.Indent) - instantiate(makeResourceName(resource.Name(), suffix)) - g.Fgenf(w, ";\n\n") - g.Fgenf(w, "%sresources.add(resource);\n", g.Indent) - }) - g.Fgenf(w, "%s}\n\n", g.Indent) - g.Fgenf(w, "%sreturn resources;\n", g.Indent) - }) - g.Fgenf(w, "%s});\n\n", g.Indent) - return - } - // not an async function invoke - // wrap into range collection - g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr) - } else { - // wrap into range collection - g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr) - } + switch expr := resource.Options.Range.(type) { + case *model.ScopeTraversalExpression: + traversalExpr := expr + if len(traversalExpr.Parts) == 2 { + // Meaning here we have {root}.{part} expression which the most common + // check whether {root} is actually a variable name that holds the result + // of a function invoke + if functionSchema, isInvoke := g.functionInvokes[traversalExpr.RootName]; isInvoke { + resultTypeName := names.LowerCamelCase(typeName(functionSchema.Outputs)) + part := getTraversalKey(traversalExpr.Traversal.SimpleSplit().Rel) + g.genIndent(w) + g.Fgenf(w, "final var %s = ", resource.Name()) + g.Fgenf(w, "%s.applyValue(%s -> {\n", traversalExpr.RootName, resultTypeName) + g.Indented(func() { + g.Fgenf(w, "%sfinal var resources = new ArrayList<%s>();\n", g.Indent, resourceTypeName) + g.Fgenf(w, "%sfor (var range : KeyedValue.of(%s.%s())) {\n", g.Indent, resultTypeName, part) + g.Indented(func() { + suffix := "range.key()" + g.Fgenf(w, "%svar resource = ", g.Indent) + instantiate(makeResourceName(resource.Name(), suffix)) + g.Fgenf(w, ";\n\n") + g.Fgenf(w, "%sresources.add(resource);\n", g.Indent) + }) + g.Fgenf(w, "%s}\n\n", g.Indent) + g.Fgenf(w, "%sreturn resources;\n", g.Indent) + }) + g.Fgenf(w, "%s});\n\n", g.Indent) + return } + // not an async function invoke + // wrap into range collection + g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr) + } else { // wrap into range collection g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr) - default: - // assume function call returns a Range - g.Fgenf(w, "%sfor (var range : %.12o) {\n", g.Indent, rangeExpr) } - default: // wrap into range collection g.Fgenf(w, "%sfor (var range : KeyedValue.of(%.12o)) {\n", g.Indent, rangeExpr) diff --git a/pkg/codegen/testing/test/testdata/aws-eks-pp/java/aws-eks.java b/pkg/codegen/testing/test/testdata/aws-eks-pp/java/aws-eks.java index a03727fe866..f0682eea918 100644 --- a/pkg/codegen/testing/test/testdata/aws-eks-pp/java/aws-eks.java +++ b/pkg/codegen/testing/test/testdata/aws-eks-pp/java/aws-eks.java @@ -71,23 +71,37 @@ public static void stack(Context ctx) { final var zones = AwsFunctions.getAvailabilityZones(GetAvailabilityZonesArgs.builder() .build()); - for (var range : KeyedValue.of(zones.applyValue(_zones -> _zones.names()))) { - new Subnet("vpcSubnet-" + range.key(), SubnetArgs.builder() - .assignIpv6AddressOnCreation(false) - .vpcId(eksVpc.id()) - .mapPublicIpOnLaunch(true) - .cidrBlock(String.format("10.100.%s.0/24", range.key())) - .availabilityZone(range.value()) - .tags(Map.of("Name", String.format("pulumi-sn-%s", range.value()))) - .build()); - } - - for (var range : KeyedValue.of(zones.applyValue(_zones -> _zones.names()))) { - new RouteTableAssociation("rta-" + range.key(), RouteTableAssociationArgs.builder() - .routeTableId(eksRouteTable.id()) - .subnetId(vpcSubnet.applyValue(_vpcSubnet -> _vpcSubnet[range.key()].id())) - .build()); - } + final var vpcSubnet = zones.applyValue(getAvailabilityZonesResult -> { + final var resources = new ArrayList(); + for (var range : KeyedValue.of(getAvailabilityZonesResult.names())) { + var resource = new Subnet("vpcSubnet-" + range.key(), SubnetArgs.builder() + .assignIpv6AddressOnCreation(false) + .vpcId(eksVpc.id()) + .mapPublicIpOnLaunch(true) + .cidrBlock(String.format("10.100.%s.0/24", range.key())) + .availabilityZone(range.value()) + .tags(Map.of("Name", String.format("pulumi-sn-%s", range.value()))) + .build()); + + resources.add(resource); + } + + return resources; + }); + + final var rta = zones.applyValue(getAvailabilityZonesResult -> { + final var resources = new ArrayList(); + for (var range : KeyedValue.of(getAvailabilityZonesResult.names())) { + var resource = new RouteTableAssociation("rta-" + range.key(), RouteTableAssociationArgs.builder() + .routeTableId(eksRouteTable.id()) + .subnetId(vpcSubnet.applyValue(_vpcSubnet -> _vpcSubnet[range.key()].id())) + .build()); + + resources.add(resource); + } + + return resources; + }); final var subnetIds = vpcSubnet.applyValue(_vpcSubnet -> _vpcSubnet.stream().map(element -> element.id()).collect(toList())); diff --git a/pkg/codegen/testing/test/testdata/aws-s3-folder-pp/java/aws-s3-folder.java b/pkg/codegen/testing/test/testdata/aws-s3-folder-pp/java/aws-s3-folder.java index f4ea68779ae..b5e33c530b6 100644 --- a/pkg/codegen/testing/test/testdata/aws-s3-folder-pp/java/aws-s3-folder.java +++ b/pkg/codegen/testing/test/testdata/aws-s3-folder-pp/java/aws-s3-folder.java @@ -37,7 +37,7 @@ public static void stack(Context ctx) { final var siteDir = "www"; // For each file in the directory, create an S3 object stored in `siteBucket` - for (var range : readDir(siteDir)) { + for (var range : KeyedValue.of(readDir(siteDir))) { new BucketObject("files-" + range.key(), BucketObjectArgs.builder() .bucket(siteBucket.id()) .key(range.value()) diff --git a/pkg/codegen/testing/test/testdata/simple-invoke-with-range-pp/java/simple-invoke-with-range.java b/pkg/codegen/testing/test/testdata/simple-invoke-with-range-pp/java/simple-invoke-with-range.java index 5ee6ff88648..c8383fda99a 100644 --- a/pkg/codegen/testing/test/testdata/simple-invoke-with-range-pp/java/simple-invoke-with-range.java +++ b/pkg/codegen/testing/test/testdata/simple-invoke-with-range-pp/java/simple-invoke-with-range.java @@ -24,15 +24,22 @@ public static void stack(Context ctx) { final var zones = AwsFunctions.getAvailabilityZones(GetAvailabilityZonesArgs.builder() .build()); - for (var range : KeyedValue.of(zones.applyValue(_zones -> _zones.names()))) { - new Subnet("vpcSubnet-" + range.key(), SubnetArgs.builder() - .assignIpv6AddressOnCreation(false) - .mapPublicIpOnLaunch(true) - .cidrBlock(String.format("10.100.%s.0/24", range.key())) - .availabilityZone(range.value()) - .tags(Map.of("Name", String.format("pulumi-sn-%s", range.value()))) - .build()); - } + final var vpcSubnet = zones.applyValue(getAvailabilityZonesResult -> { + final var resources = new ArrayList(); + for (var range : KeyedValue.of(getAvailabilityZonesResult.names())) { + var resource = new Subnet("vpcSubnet-" + range.key(), SubnetArgs.builder() + .assignIpv6AddressOnCreation(false) + .mapPublicIpOnLaunch(true) + .cidrBlock(String.format("10.100.%s.0/24", range.key())) + .availabilityZone(range.value()) + .tags(Map.of("Name", String.format("pulumi-sn-%s", range.value()))) + .build()); + + resources.add(resource); + } + + return resources; + }); } }