Skip to content

Commit

Permalink
fix: use both attribute and block types to find if an attribute is de…
Browse files Browse the repository at this point in the history
…fined
  • Loading branch information
DanielMSchmidt committed Jun 14, 2022
1 parent 3f0cc7e commit f78dd8c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
24 changes: 17 additions & 7 deletions packages/@cdktf/hcl2cdk/lib/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {
TerraformModuleConstraint,
escapeAttributeName,
} from "@cdktf/provider-generator";
import { getBlockTypeAtPath, getAttributeTypeAtPath } from "./provider";
import {
getBlockTypeAtPath,
getAttributeTypeAtPath,
getTypeAtPath,
} from "./provider";

function getReference(graph: DirectedGraph, id: string) {
logger.debug(`Finding reference for ${id}`);
Expand Down Expand Up @@ -124,17 +128,23 @@ export const valueToTs = async (
const isMapAttribute = Array.isArray(attribute?.type)
? attribute?.type?.[0] === "map"
: false;

const typeMetadata = getTypeAtPath(
scope.providerSchema,
itemPath
);

const isSingleItemBlock =
typeMetadata !== null && "max_items" in typeMetadata
? typeMetadata?.max_items === 1
: false;
const shouldBeArray =
typeof value === "object" &&
!Array.isArray(value) &&
getBlockTypeAtPath(scope.providerSchema, itemPath)
?.max_items !== 1 &&
!isSingleItemBlock &&
!isMapAttribute &&
key !== "tags";

const isNeitherBlockNorAttribute =
getBlockTypeAtPath(scope.providerSchema, itemPath) === null &&
getAttributeTypeAtPath(scope.providerSchema, itemPath) === null;
const isNeitherBlockNorAttribute = !typeMetadata;

return t.objectProperty(
t.stringLiteral(
Expand Down
34 changes: 34 additions & 0 deletions packages/@cdktf/hcl2cdk/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ export function getAttributeTypeAtPath(
return attributes[attributeName];
}

export function getTypeAtPath(schema: ProviderSchema, path: string) {
const resourceSchema = getResourceAtPath(schema, path);
if (!resourceSchema) {
return null;
}
const { resource, parts } = resourceSchema;

let currentSchema: BlockType | typeof resource = resource;
do {
const part = parts.shift() as string;
if (
!currentSchema.block.block_types ||
!currentSchema.block.block_types.hasOwnProperty(part)
) {
// Found no block property with this name, there could be an attribute
if (
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;
}
}
}

currentSchema = currentSchema.block.block_types[part];
} while (parts.length > 0);

return currentSchema;
}

type Plan = z.infer<typeof schema>;
export function getProviderRequirements(plan: Plan) {
// In Terraform one can implicitly define the provider by using resources of that type
Expand Down
2 changes: 0 additions & 2 deletions packages/@cdktf/hcl2cdk/test/hcl2cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1400,8 +1400,6 @@ describe("convert", () => {
host = self.public_ip
}
}
`,
Synth.never
);
Expand Down

0 comments on commit f78dd8c

Please sign in to comment.