Skip to content

Commit

Permalink
fix(hcl2cdk): use the same attribute renaming function as in the prov…
Browse files Browse the repository at this point in the history
…ider generation

Closes #1708
  • Loading branch information
DanielMSchmidt committed Jun 2, 2022
1 parent 1788df6 commit 748a21d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
9 changes: 7 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
constructAst,
isListExpression,
} from "./expressions";
import { TerraformModuleConstraint } from "@cdktf/provider-generator";
import {
TerraformModuleConstraint,
escapeAttributeName,
} from "@cdktf/provider-generator";
import { getBlockTypeAtPath } from "./provider";

function getReference(graph: DirectedGraph, id: string) {
Expand Down Expand Up @@ -107,7 +110,9 @@ export const valueToTs = (
key !== "tags";

return t.objectProperty(
t.stringLiteral(key !== "for_each" ? camelCase(key) : key),
t.stringLiteral(
key !== "for_each" ? escapeAttributeName(camelCase(key)) : key
),
shouldBeArray
? t.arrayExpression([
valueToTs(scope, value, itemPath, nodeIds, scopedIds),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ new aws.guardduty.GuarddutyFilter(this, \\"MyFilter\\", {
findingCriteria: {
criterion: [
{
equals: [\\"eu-west-1\\"],
equalTo: [\\"eu-west-1\\"],
field: \\"region\\",
},
{
Expand Down
3 changes: 1 addition & 2 deletions packages/@cdktf/hcl2cdk/test/hcl2cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum Synth {
yes,
needsAFix_BooleanAsIResolvable, // https://github.com/hashicorp/terraform-cdk/issues/1550
needsAFix_UnforseenClassRename, // https://github.com/hashicorp/terraform-cdk/issues/1552
needsAFix_UnforseenPropertyRename, // https://github.com/hashicorp/terraform-cdk/issues/1708
never, // Some examples are built so that they will never synth but test a specific generation edge case
}

Expand Down Expand Up @@ -1334,7 +1333,7 @@ describe("convert", () => {
}
}
`,
Synth.needsAFix_UnforseenPropertyRename
Synth.yes
);

const targetLanguages = ["typescript", "python", "csharp", "java"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export interface AttributeModelOptions {
required: boolean;
}

export function escapeAttributeName(name: string) {
// `self` and `build` doesn't work in as property name in Python
if (name === "self" || name === "build") return `${name}Attribute`;
// jsii can't handle `getFoo` properties, since it's incompatible with Java
if (name.match(/^get[A-Za-z]+/)) return name.replace("get", "fetch");
// `equals` is a prohibited name in jsii
if (name === "equals") return "equalTo";
// `node` is already used by the Constructs base class
if (name === "node") return "nodeAttribute";
// `System` shadows built-in types in CSharp (see #1420)
if (name === "system") return "systemAttribute";
// `tfResourceType` is already used by resources to distinguish between different resource types
if (name === "tfResourceType") return `${name}Attribute`;
return name;
}

export class AttributeModel {
public storageName: string; // private property
private _name: string;
Expand Down Expand Up @@ -196,19 +212,7 @@ export class AttributeModel {
}

public static escapeName(name: string): string {
// `self` and `build` doesn't work in as property name in Python
if (name === "self" || name === "build") return `${name}Attribute`;
// jsii can't handle `getFoo` properties, since it's incompatible with Java
if (name.match(/^get[A-Za-z]+/)) return name.replace("get", "fetch");
// `equals` is a prohibited name in jsii
if (name === "equals") return "equalTo";
// `node` is already used by the Constructs base class
if (name === "node") return "nodeAttribute";
// `System` shadows built-in types in CSharp (see #1420)
if (name === "system") return "systemAttribute";
// `tfResourceType` is already used by resources to distinguish between different resource types
if (name === "tfResourceType") return `${name}Attribute`;
return name;
return escapeAttributeName(name);
}

public get description(): string | undefined {
Expand Down
1 change: 1 addition & 0 deletions packages/@cdktf/provider-generator/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
import { CodeMaker } from "codemaker";
import * as srcmak from "jsii-srcmak";
import { generateJsiiLanguage } from "./get/constructs-maker";
export { escapeAttributeName } from "./get/generator/models";
import { TerraformProviderGenerator } from "./get/generator/provider-generator";
export { setLogger } from "./config";

Expand Down

0 comments on commit 748a21d

Please sign in to comment.