Skip to content

Commit

Permalink
lib: render empty string as quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jul 29, 2024
1 parent 91ec8b0 commit 6b78c57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/cdktf/lib/hcl/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function wrapIdentifierInQuotesIfNeeded(key: string): string {
*
*/
function renderString(str: string): string {
if (str === "") {
return `""`;
}

if (!str) {
return str;
}
Expand Down Expand Up @@ -526,6 +530,10 @@ function renderFuzzyJsonExpression(jsonExpression: any): string {
}

if (typeof jsonExpression === "string") {
if (jsonExpression === "") {
return `""`;
}

if (jsonExpression.includes("${")) {
return `"${jsonExpression}"`;
}
Expand Down Expand Up @@ -609,7 +617,7 @@ export function renderAttributes(attributes: any): string {
!v.hasOwnProperty("dynamic")
) {
if (metaBlocks.includes(name)) {
return `${name} {
return `${name} {
${renderSimpleAttributes(v)}
}`;
}
Expand Down Expand Up @@ -643,8 +651,8 @@ ${renderSimpleAttributes(v)}
}

if (block && type !== "list" && type !== "set") {
return `${name} {
${renderAttributes(value)}
return `${name} {
${renderAttributes(value)}
}`;
}
if (type === "list" || type === "set") {
Expand Down
27 changes: 27 additions & 0 deletions packages/cdktf/test/json-to-hcl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ test("string local with quoted name", async () => {
`);
});

test("empty string", async () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformLocal(stack, "greeting", {
a: "",
});

new TestResource(stack, "test", {
name: "",
});

const hcl = Testing.synthHcl(stack);
expect(hcl).toMatchInlineSnapshot(`
"
locals {
greeting = {
a = ""
}
}
resource "test_resource" "test" {
name = ""
}"
`);
});

test("with provider alias", async () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");
Expand Down

0 comments on commit 6b78c57

Please sign in to comment.