Skip to content

Commit

Permalink
simplify invokes inside range expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-Ajaj committed Jan 11, 2025
1 parent cda98a7 commit 1c13cc0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 73 deletions.
78 changes: 32 additions & 46 deletions pkg/codegen/java/gen_program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
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)
Expand Down
48 changes: 31 additions & 17 deletions pkg/codegen/testing/test/testdata/aws-eks-pp/java/aws-eks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subnet>();
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<RouteTableAssociation>();
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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Subnet>();
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;
});

}
}

0 comments on commit 1c13cc0

Please sign in to comment.