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

fix(provider-generator): Replace jsii incompatible names #1516

Merged
merged 4 commits into from
Feb 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -7933,6 +7933,39 @@ export class Eks extends TerraformModule {
"
`;

exports[`getX variables 1`] = `
"// generated by cdktf get
// ./module
import { TerraformModule } from 'cdktf';
import { Construct } from 'constructs';
export interface ModuleOptions {
/**
* Whether to get AWS account ID, User ID, and ARN in which Terraform is authorized
* @default true
*/
readonly fetchCallerIdentity?: boolean;
}
export class Module extends TerraformModule {
private readonly inputs: { [name: string]: any } = { }
public constructor(scope: Construct, id: string, options: ModuleOptions = {}) {
super(scope, id, {
source: './module',
});
this.fetchCallerIdentity = options.fetchCallerIdentity;
}
public get fetchCallerIdentity(): boolean | undefined {
return this.inputs['get_caller_identity'] as boolean | undefined;
}
public set fetchCallerIdentity(value: boolean | undefined) {
this.inputs['get_caller_identity'] = value;
}
protected synthesizeAttributes() {
return this.inputs;
}
}
"
`;

exports[`no module outputs 1`] = `
"// generated by cdktf get
// ./module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "get_caller_identity" {
description = "Whether to get AWS account ID, User ID, and ARN in which Terraform is authorized"
type = bool
default = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ test("generate multiple aws modules", async () => {
);
expect(rdsOutput).toMatchSnapshot();
});

expectModuleToMatchSnapshot("getX variables", "generator", [
"module-get-x.test.fixture.tf",
]);
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,23 @@ export class AttributeModel {
}

public get name(): string {
return AttributeModel.escapeName(this._name);
}

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

public get description(): string | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CodeMaker, toCamelCase } from "codemaker";
import { ConstructsMakerModuleTarget } from "../constructs-maker";
import { AttributeModel } from "./models";

export class ModuleGenerator {
constructor(
Expand Down Expand Up @@ -41,9 +42,9 @@ export class ModuleGenerator {
}
this.code.line(` */`);
this.code.line(
`readonly ${toCamelCase(input.name)}${optional}: ${parseType(
input.type
)};`
`readonly ${AttributeModel.escapeName(
toCamelCase(input.name)
)}${optional}: ${parseType(input.type)};`
);
}
this.code.closeBlock();
Expand All @@ -65,14 +66,14 @@ export class ModuleGenerator {
this.code.close(`});`);

for (const input of spec.inputs) {
const inputName = toCamelCase(input.name);
const inputName = AttributeModel.escapeName(toCamelCase(input.name));
this.code.line(`this.${inputName} = options.${inputName};`);
}

this.code.close(`}`); // ctor

for (const input of spec.inputs) {
const inputName = toCamelCase(input.name);
const inputName = AttributeModel.escapeName(toCamelCase(input.name));
const inputType =
parseType(input.type) +
(input.required && input.default === undefined ? "" : " | undefined");
Expand Down