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

feat(hcl2cdk): use Fn.lookupNested() instead of propertyAccess() #3056

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions packages/@cdktf/hcl2cdk/lib/__tests__/expressionToTs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(foo, ["*", "id"]))"`
`"Token.asString(Fn.lookupNested(foo, ["*", "id"]))"`
);
});

Expand All @@ -434,7 +434,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
`"Token.asString(Fn.lookupNested(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
);
});

Expand All @@ -448,7 +448,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
`"Token.asString(Fn.lookupNested(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
);
});

Expand All @@ -462,7 +462,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
`"Token.asString(Fn.lookupNested(examplebucket.networkInterface, ["0", "access_config", "0", "assigned_nat_ip"]))"`
);
});

Expand All @@ -475,7 +475,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.toset(propertyAccess(examplebucket, ["*"])))"`
`"Token.asString(Fn.toset(Fn.lookupNested(examplebucket, ["*"])))"`
);
});

Expand All @@ -502,7 +502,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(Fn.element(examplebucket, 0), ["id"]))"`
`"Token.asString(Fn.lookupNested(Fn.element(examplebucket, 0), ["id"]))"`
);
});

Expand All @@ -515,7 +515,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.element(propertyAccess(examplebucket, ["*", "id"]), 0))"`
`"Token.asString(Fn.element(Fn.lookupNested(examplebucket, ["*", "id"]), 0))"`
);
});

Expand Down Expand Up @@ -584,7 +584,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(examplebucket, ["0", "id"]))"`
`"Token.asString(Fn.lookupNested(examplebucket, ["0", "id"]))"`
);
});

Expand Down Expand Up @@ -646,7 +646,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.element(propertyAccess(test2.value, ["\\"val1\\""]), 0))"`
`"Token.asString(Fn.element(Fn.lookupNested(test2.value, ["\\"val1\\""]), 0))"`
);
});

Expand All @@ -659,7 +659,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.flatten(propertyAccess(vnets.value, ["*", "subnets", "*", "name"])))"`
`"Token.asString(Fn.flatten(Fn.lookupNested(vnets.value, ["*", "subnets", "*", "name"])))"`
);
});

Expand All @@ -677,7 +677,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`""\${{ for vnet in \${" + propertyAccess(vnets.value, ["*"]) + "} : (vnet.vnet_name) => vnet.subnets[*].name}}""`
`""\${{ for vnet in \${" + Fn.lookupNested(vnets.value, ["*"]) + "} : (vnet.vnet_name) => vnet.subnets[*].name}}""`
);
});

Expand Down Expand Up @@ -779,7 +779,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.join("-", Token.asList([propertyAccess(tags.value, ["app"]), propertyAccess(tags.value, ["env"])])))"`
`"Token.asString(Fn.join("-", Token.asList([Fn.lookupNested(tags.value, ["app"]), Fn.lookupNested(tags.value, ["env"])])))"`
);
});

Expand All @@ -793,7 +793,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.join("-", Token.asList(Fn.concat([propertyAccess(tags.value, ["app"]), propertyAccess(tags.value, ["env"]), propertyAccess(tags.value, ["other"])]))))"`
`"Token.asString(Fn.join("-", Token.asList(Fn.concat([Fn.lookupNested(tags.value, ["app"]), Fn.lookupNested(tags.value, ["env"]), Fn.lookupNested(tags.value, ["other"])]))))"`
);
});

Expand Down Expand Up @@ -845,7 +845,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(myIterator.value, ["name"]))"`
`"Token.asString(Fn.lookupNested(myIterator.value, ["name"]))"`
);
});

Expand All @@ -858,7 +858,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(Fn.element(examplebucket, 0), ["id"]))"`
`"Token.asString(Fn.lookupNested(Fn.element(examplebucket, 0), ["id"]))"`
);
});

Expand All @@ -873,7 +873,7 @@ describe("expressionToTs", () => {
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(Fn.concat([propertyAccess(privateSubnets.value, ["*", "id"]), propertyAccess(publicSubnets.value, ["*", "id"])]))"`
`"Token.asString(Fn.concat([Fn.lookupNested(privateSubnets.value, ["*", "id"]), Fn.lookupNested(publicSubnets.value, ["*", "id"])]))"`
);
});

Expand Down Expand Up @@ -1003,7 +1003,7 @@ EOF`;
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(changemeAzListEbsSnapshot.names, ["0"]))"`
`"Token.asString(Fn.lookupNested(changemeAzListEbsSnapshot.names, ["0"]))"`
);
});

Expand All @@ -1019,7 +1019,7 @@ EOF`;
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(changemeAzListEbsSnapshot, ["testing_map", "foo"]))"`
`"Token.asString(Fn.lookupNested(changemeAzListEbsSnapshot, ["testing_map", "foo"]))"`
);
});

Expand Down Expand Up @@ -1053,7 +1053,7 @@ EOF`;
() => ["map", "string"]
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asStringMap(propertyAccess(changemeExternalThumbprintData, ["result", "thumbprint"]))"`
`"Token.asStringMap(Fn.lookupNested(changemeExternalThumbprintData, ["result", "thumbprint"]))"`
);
});

Expand All @@ -1069,7 +1069,7 @@ EOF`;
() => "string"
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(examplebucket, ["foo", "bar"]))"`
`"Token.asString(Fn.lookupNested(examplebucket, ["foo", "bar"]))"`
);
});

Expand All @@ -1086,7 +1086,7 @@ EOF`;
() => "string"
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(defaultTags.value, ["project"])) + "-client-tg""`
`"Token.asString(Fn.lookupNested(defaultTags.value, ["project"])) + "-client-tg""`
);
});

Expand Down Expand Up @@ -1115,7 +1115,7 @@ EOF`;
getType
);
expect(code(result)).toMatchInlineSnapshot(
`"Token.asString(propertyAccess(available.names, ["\${count.index}"]))"`
`"Token.asString(Fn.lookupNested(available.names, ["\${count.index}"]))"`
);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/__tests__/expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,15 @@ describe("expressions", () => {

it("should convert iterator value deep accessor", async () => {
expect(await run('"${each.value.list.map.name}"')).toMatchInlineSnapshot(
`"propertyAccess(myIterator.value, ["list", "map", "name"]);"`
`"Fn.lookupNested(myIterator.value, ["list", "map", "name"]);"`
);
});

it("should convert iterator value with map access", async () => {
expect(
await run('"${each.value[0]["map"]["name"]}"')
).toMatchInlineSnapshot(
`"propertyAccess(myIterator.value, ["[0]", "[\\"map\\"]", "[\\"name\\"]"]);"`
`"Fn.lookupNested(myIterator.value, ["[0]", "[\\"map\\"]", "[\\"name\\"]"]);"`
);
});
});
Expand Down
49 changes: 36 additions & 13 deletions packages/@cdktf/hcl2cdk/lib/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,14 @@ function convertScopeTraversalExpressionToTs(

if (segments.length > 2) {
scope.importables.push({
constructName: "propertyAccess",
constructName: "Fn",
provider: "cdktf",
});
return t.callExpression(t.identifier("propertyAccess"), [
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);
return t.callExpression(callee, [
variableAccessor,
t.arrayExpression(
segments.slice(2).map((s) => t.stringLiteral(s.segment))
Expand Down Expand Up @@ -340,10 +344,14 @@ function convertScopeTraversalExpressionToTs(
}

scope.importables.push({
constructName: "propertyAccess",
constructName: "Fn",
provider: "cdktf",
});
return t.callExpression(t.identifier("propertyAccess"), [
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);
return t.callExpression(callee, [
ref,
t.arrayExpression(nonRefSegments.map((s) => t.stringLiteral(s.segment))),
]);
Expand Down Expand Up @@ -598,11 +606,14 @@ function convertIndexExpressionToTs(
const keyExpression = convertTFExpressionAstToTs(scope, keyExpressionChild!);

scope.importables.push({
constructName: "propertyAccess",
constructName: "Fn",
provider: "cdktf",
});

return t.callExpression(t.identifier("propertyAccess"), [
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);
return t.callExpression(callee, [
collectionExpression,
t.arrayExpression([keyExpression]),
]);
Expand Down Expand Up @@ -631,11 +642,15 @@ function convertSplatExpressionToTs(

const segments = relativeExpression.split(/\.|\[|\]/).filter((s) => s);
scope.importables.push({
constructName: "propertyAccess",
constructName: "Fn",
provider: "cdktf",
});
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);

return t.callExpression(t.identifier("propertyAccess"), [
return t.callExpression(callee, [
sourceExpression,
t.arrayExpression([
// we don't need to use the anonSymbolExpression here because
Expand Down Expand Up @@ -708,11 +723,15 @@ function convertRelativeTraversalExpressionToTs(
);

scope.importables.push({
constructName: "propertyAccess",
constructName: "Fn",
provider: "cdktf",
});
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);

return t.callExpression(t.identifier("propertyAccess"), [
return t.callExpression(callee, [
source,
t.arrayExpression(segments.map((s) => t.stringLiteral(s.segment))),
]);
Expand Down Expand Up @@ -922,10 +941,14 @@ export function dynamicVariableToAst(
) {
const segmentsAfterEachValue = segments.slice(2);
scope.importables.push({
constructName: "Fn",
provider: "cdktf",
constructName: "propertyAccess",
});
return t.callExpression(t.identifier("propertyAccess"), [
const callee = t.memberExpression(
t.identifier("Fn"),
t.identifier("lookupNested")
);
return t.callExpression(callee, [
t.memberExpression(t.identifier(iteratorName), t.identifier("value")),
t.arrayExpression(
segmentsAfterEachValue.map((part) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VariableType,
TerraformVariable,
TerraformOutput,
propertyAccess,
Fn,
TerraformStack,
} from "cdktf";
/*
Expand All @@ -29,7 +29,7 @@ class MyConvertedCode extends TerraformStack {
program: ["\${path.module}/oidc_thumbprint.sh", fargateRegion.value],
});
new TerraformOutput(this, "foo", {
value: propertyAccess(dataExternalThumbprint, ["result", "thumbprint"]),
value: Fn.lookupNested(dataExternalThumbprint, ["result", "thumbprint"]),
});
}
}
Expand Down
Loading