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

[pcl/nodejs] Support range expressions that are of type output #12749

Merged
merged 1 commit into from Apr 28, 2023

Conversation

Zaid-Ajaj
Copy link
Contributor

@Zaid-Ajaj Zaid-Ajaj commented Apr 26, 2023

Description

This PR implements nodejs program-gen support for range expressions that are of type Output<T> where T is a collection that should be iterated inside an apply call.

Example PCL
resource "root" "range:index:Root" {}

// creating resources by iterating a property of type array(string) of another resource
resource "fromListOfStrings" "range:index:Example" {
  options {
    range = root.arrayOfString
  }

  someString = range.value
}

// creating resources by iterating a property of type map(string) of another resource
resource "fromMapOfStrings" "range:index:Example" {
  options {
    range = root.mapOfString
  }

  someString = "${range.key} ${range.value}"
}

// computed range list expression to create instances of range:index:Example resource
resource "fromComputedListOfStrings" "range:index:Example" {
  options {
    range = [
        root.mapOfString["hello"],
        root.mapOfString["world"]
    ]
  }

  someString = "${range.key} ${range.value}"
}

// computed range for expression to create instances of range:index:Example resource
resource "fromComputedForExpression" "range:index:Example" {
  options {
    range = [for value in root.arrayOfString : root.mapOfString[value]]
  }

  someString = "${range.key} ${range.value}"
}
Generated TypeScript
import * as pulumi from "@pulumi/pulumi";
import * as range from "@pulumi/range";

const root = new range.Root("root", {});
// creating resources by iterating a property of type array(string) of another resource
const fromListOfStrings: range.Example[] = [];
root.arrayOfString.apply(rangeBody => {
    for (const range of rangeBody.map((v, k) => ({key: k, value: v}))) {
        fromListOfStrings.push(new range.Example(`fromListOfStrings-${range.key}`, {someString: range.value}));
    }
});
// creating resources by iterating a property of type map(string) of another resource
const fromMapOfStrings: range.Example[] = [];
root.mapOfString.apply(rangeBody => {
    for (const range of Object.entries(rangeBody).map(([k, v]) => ({key: k, value: v}))) {
        fromMapOfStrings.push(new range.Example(`fromMapOfStrings-${range.key}`, {someString: `${range.key} ${range.value}`}));
    }
});
// computed range list expression to create instances of range:index:Example resource
const fromComputedListOfStrings: range.Example[] = [];
pulumi.all([
    root.mapOfString.apply(mapOfString => mapOfString?.hello),
    root.mapOfString.apply(mapOfString => mapOfString?.world),
]).apply(rangeBody => {
    for (const range of rangeBody.map((v, k) => ({key: k, value: v}))) {
        fromComputedListOfStrings.push(new range.Example(`fromComputedListOfStrings-${range.key}`, {someString: `${range.key} ${range.value}`}));
    }
});
// computed range for expression to create instances of range:index:Example resource
const fromComputedForExpression: range.Example[] = [];
pulumi.all([root.arrayOfString, root.mapOfString]).apply(([arrayOfString, mapOfString]) => {
    for (const range of arrayOfString.map(value => (mapOfString[value])).map((v, k) => ({key: k, value: v}))) {
        fromComputedForExpression.push(new range.Example(`fromComputedForExpression-${range.key}`, {someString: `${range.key} ${range.value}`}));
    }
});

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • I have run make changelog and committed the changelog/pending/<file> documenting my change
  • Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version

@pulumi-bot
Copy link
Contributor

pulumi-bot commented Apr 26, 2023

Changelog

[uncommitted] (2023-04-26)

Features

  • [programgen/nodejs] Support range expressions that are of type output
    #12749

@@ -177,7 +177,7 @@ func (g *generator) GenForExpression(w io.Writer, expr *model.ForExpression) {

if expr.Key != nil {
// TODO(pdg): grouping
g.Fgenf(w, ".reduce((__obj, %s) => { ...__obj, [%.v]: %.v })", reduceParams, expr.Key, expr.Value)
g.Fgenf(w, ".reduce((__obj, %s) => ({ ...__obj, [%.v]: %.v }))", reduceParams, expr.Key, expr.Value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small fix for this expression where it was missing the extra parenthesis around the returned object, otherwise it is invalid TypeScript

@Zaid-Ajaj Zaid-Ajaj force-pushed the zaid/eventual-range-expressions branch from d2a261a to 1ebf015 Compare April 26, 2023 15:50
@Zaid-Ajaj Zaid-Ajaj marked this pull request as ready for review April 26, 2023 15:54
@Zaid-Ajaj Zaid-Ajaj force-pushed the zaid/eventual-range-expressions branch from 8d39416 to e9c9fb5 Compare April 26, 2023 18:00
Copy link
Member

@Frassle Frassle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests look reasonable

@Zaid-Ajaj
Copy link
Contributor Author

bors merge

@bors
Copy link
Contributor

bors bot commented Apr 28, 2023

Build succeeded:

  • bors-ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants