Skip to content

Commit

Permalink
fix: fix map and nested attribute special cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jun 15, 2022
1 parent 24d99f8 commit 4688e70
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
6 changes: 4 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ export const valueToTs = async (
);

const isSingleItemBlock =
typeMetadata !== null && "max_items" in typeMetadata
? typeMetadata?.max_items === 1
typeMetadata &&
typeof typeMetadata === "object" &&
typeMetadata.hasOwnProperty("max_items")
? (typeMetadata as any).max_items === 1
: false;

const shouldBeArray =
Expand Down
58 changes: 51 additions & 7 deletions packages/@cdktf/hcl2cdk/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ProviderSchema,
BlockType,
Attribute,
AttributeType,
} from "@cdktf/provider-generator";
import { schema } from "./schema";

Expand Down Expand Up @@ -113,7 +114,55 @@ export function getAttributeTypeAtPath(
return attributes[attributeName];
}

export function getTypeAtPath(schema: ProviderSchema, path: string): any {
// Resolves within a list of objects, e.g.
// "ingress": {
// "type": [
// "set",
// [
// "object",
// {
// "cidr_blocks": [
// "list",
// "string"
// ],
function resolveAttribute(
att: Attribute,
parts: string[]
): AttributeType | null | undefined {
if (parts.length === 0) {
return att.type;
}

let currentAtt: AttributeType | undefined = att.type;
do {
const part = parts.shift() as string;
if (
Array.isArray(currentAtt) &&
currentAtt.length === 2 &&
(currentAtt[0] === "set" || currentAtt[0] === "list") &&
Array.isArray(currentAtt[1]) &&
currentAtt[1][0] === "object"
) {
// We can go deeper into the set/list
const x = currentAtt[1][1][part];
currentAtt = x;
} else {
return null;
}
} while (parts.length > 0);

if (parts.length === 0) {
return currentAtt;
} else {
// We could not go deeper but the item path expects more parts, we have to return null
return null;
}
}

export function getTypeAtPath(
schema: ProviderSchema,
path: string
): BlockType | AttributeType | null | undefined {
const resourceSchema = getResourceAtPath(schema, path);
if (!resourceSchema) {
return null;
Expand All @@ -132,12 +181,7 @@ export function getTypeAtPath(schema: ProviderSchema, path: string): any {
currentSchema.block.attributes &&
currentSchema.block.attributes.hasOwnProperty(part)
) {
if (parts.length === 0) {
return currentSchema.block.attributes[part];
} else {
// We found an attribute, but the path goes deeper so we have no information
return null;
}
return resolveAttribute(currentSchema.block.attributes[part], parts);
}
}

Expand Down
18 changes: 9 additions & 9 deletions packages/@cdktf/hcl2cdk/test/__snapshots__/hcl2cdk.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,11 @@ exports[`convert modules snapshot 1`] = `
new Vpc.Vpc(this, \\"vpc\\", {
azs: [\\"eu-west-1a\\", \\"eu-west-1b\\", \\"eu-west-1c\\"],
cidr: \\"10.0.0.0/16\\",
enable_nat_gateway: true,
enable_vpn_gateway: true,
enableNatGateway: true,
enableVpnGateway: true,
name: \\"my-vpc\\",
private_subnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"],
public_subnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"],
privateSubnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"],
publicSubnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"],
tags: {
Environment: \\"dev\\",
Terraform: \\"true\\",
Expand Down Expand Up @@ -1034,11 +1034,11 @@ import * as Vpc from \\"./.gen/modules/terraform-aws-modules/aws/vpc\\";
const vpc = new Vpc.Vpc(this, \\"vpc\\", {
azs: [\\"eu-west-1a\\", \\"eu-west-1b\\", \\"eu-west-1c\\"],
cidr: \\"10.0.0.0/16\\",
enable_nat_gateway: true,
enable_vpn_gateway: true,
enableNatGateway: true,
enableVpnGateway: true,
name: \\"my-vpc\\",
private_subnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"],
public_subnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"],
privateSubnets: [\\"10.0.1.0/24\\", \\"10.0.2.0/24\\", \\"10.0.3.0/24\\"],
publicSubnets: [\\"10.0.101.0/24\\", \\"10.0.102.0/24\\", \\"10.0.103.0/24\\"],
tags: {
Environment: \\"dev\\",
Terraform: \\"true\\",
Expand Down Expand Up @@ -1241,7 +1241,7 @@ new aws.vpc.Vpc(this, \\"example\\", {
exports[`convert terraform workspace snapshot 1`] = `
"import * as MyModule from \\"./.gen/modules/my-module\\";
new MyModule.MyModule(this, \\"example\\", {
name_prefix: \\"app-\${terraform.workspace}\\",
namePrefix: \\"app-\${terraform.workspace}\\",
});
"
`;
Expand Down

0 comments on commit 4688e70

Please sign in to comment.