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

cdktf: forExpressionForMap does not render correctly #3540

Closed
1 task
sapslaj opened this issue Mar 9, 2024 · 1 comment · Fixed by #3545
Closed
1 task

cdktf: forExpressionForMap does not render correctly #3540

sapslaj opened this issue Mar 9, 2024 · 1 comment · Fixed by #3545
Labels
bug Something isn't working cdktf confirmed independently reproduced by an engineer on the team priority/important-soon High priority, to be worked on as part of our current release or the following one.

Comments

@sapslaj
Copy link

sapslaj commented Mar 9, 2024

Expected Behavior

Based on the documentation, I would expect the follow code using TerraformIterator and forExpressionForMap to render a correct expression.

const it = TerraformIterator.fromMap({
  a: "b",
  foo: "bar",
});

// Generate a `for` expression using that iterator.
// note: using a very simple expression here for brevity.
const expr = it.forExpressionForMap("key", "val");

// Observe result
new TerraformOutput(stack, "test", {
  value: expr,
});

I would expect something akin to the following HCL:

output "test" {
  value = { for key, val in { a = "b", foo = "bar" } : key => val }
}

or in JSON

{
  "output": {
    "test": {
      "value": "${{ for key, val in { a = \"b\", foo = \"bar\" } : key => val }}"
    }
  }
}

Actual Behavior

The input map is not correctly rendered.

output "test" {
  value = "${{ for key, val in [object Object]: key => val }}"
}
{
  "output": {
    "test": {
      "value": "${{ for key, val in [object Object]: key => val }}"
    }
  }
}

Steps to Reproduce

repro.ts:

import {
  App,
  TerraformIterator,
  TerraformOutput,
  TerraformStack,
} from "cdktf";

const app = new App();
const stack = new TerraformStack(app, "sadge");

// Create an iterator based on a pre-calculated map.
const it = TerraformIterator.fromMap({
  a: "b",
  foo: "bar",
});

// Generate a `for` expression using that iterator.
// note: using a very simple expression here for brevity.
const expr = it.forExpressionForMap("key", "val");

// Observe result
new TerraformOutput(stack, "test", {
  value: expr,
});

app.synth();

npx ts-node repro.ts

Versions

cdktf debug
language: typescript
cdktf-cli: 0.20.3
node: v20.10.0
cdktf: 0.20.4
constructs: null
jsii: null
terraform: 1.6.2
arch: x64
os: linux 6.7.8-arch1-1

Providers

N/A; issue with core cdktf

Gist

No response

Possible Solutions

const input = this.resolveArg(context, this.input);

This is supposed to be returning a resolved token as a string. However, it is returning the input object as-is and that gets [object Object]'d a few lines down:

expr = `{ for ${key}, ${value} in ${input}: ${keyExpr} => ${valueExpr} }`;

Workarounds

This isn't the most ideal but building the expression by hand is a workaround.

const expr = '${{ for key, val in ' + JSON.stringify(map) + ' : key => val }}';

JSON.stringify probably isn't a great way to get that map in, but that was the easiest I could find. The logic that seems to be used for other expression builders is locked away in the un-exported TFExpression class 😞. renderMap is exported, however that implementation inserts newlines which is not desirable for this particular use case.

Anything Else?

No response

References

No response

Help Wanted

  • I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment
@sapslaj sapslaj added bug Something isn't working new Un-triaged issue labels Mar 9, 2024
@ansgarm ansgarm added cdktf confirmed independently reproduced by an engineer on the team priority/important-soon High priority, to be worked on as part of our current release or the following one. and removed new Un-triaged issue labels Mar 11, 2024
Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working cdktf confirmed independently reproduced by an engineer on the team priority/important-soon High priority, to be worked on as part of our current release or the following one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants