Skip to content

Commit a1f5549

Browse files
committed
PR feedback. More comments
1 parent b53bbce commit a1f5549

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/functions/iac/terraform.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Expression {
1212
}
1313

1414
/**
15-
*
15+
* Shorthand to create an expression that won't be quoted in the generated HCL.
1616
*/
1717
export function expr(string: string): Expression {
1818
return { "@type": "HCLExpression", value: string };
@@ -42,7 +42,9 @@ export interface Block {
4242
}
4343

4444
/**
45-
*
45+
* Copy a field from a TypeScript interface into a Terraform HCL attribute map.
46+
* Automatically converts the field name to lower snake case.
47+
* Supports an optional transform function.
4648
*/
4749
export function copyField<
4850
Kind extends string | number | boolean,
@@ -58,7 +60,9 @@ export function copyField<
5860
}
5961

6062
/**
61-
*
63+
* Moves a field from a TypeScript interface to a Terraform HCL attribute map with explicit naming.
64+
* Skips over the field if it is missing from the original input.
65+
* Supports an optional transform function.
6266
*/
6367
export function renameField<
6468
Kind extends string | number | boolean,
@@ -72,7 +76,7 @@ export function renameField<
7276
transform: (v: NonNullable<Field<Kind>>) => Value = (v) => v,
7377
): void {
7478
const val = source[sourceField];
75-
// Reset is always the behavior.
79+
// Terraform is always authorative. Leaving out val will reset the field
7680
if (val === null || val === undefined) {
7781
return;
7882
}
@@ -81,7 +85,7 @@ export function renameField<
8185
}
8286

8387
/**
84-
*
88+
* Fully qualifies project-relative SAs using the project variable.
8589
*/
8690
export function serviceAccount(sa: string): string {
8791
if (sa.endsWith("@")) {
@@ -91,7 +95,10 @@ export function serviceAccount(sa: string): string {
9195
}
9296

9397
/**
94-
*
98+
* Serializes a Terraform Value to a string.
99+
* This is the recursive function that serializes blocks.
100+
* N.B. strings must be JSON encoded (e.g. have " around them and escape other quotes)
101+
* so they can be distinguished from bare strings which are HCL expressions (e.g. var refs).
95102
*/
96103
export function serializeValue(value: Value, indentation = 0): string {
97104
if (typeof value === "string") {
@@ -107,7 +114,7 @@ export function serializeValue(value: Value, indentation = 0): string {
107114
} else if (value === null || value === undefined) {
108115
return "null";
109116
} else if (Array.isArray(value)) {
110-
if (value.some((e) => typeof e === "object")) {
117+
if (value.some((e) => e !== null && typeof e === "object")) {
111118
return `[\n${value.map((v) => " ".repeat(indentation + 1) + serializeValue(v, indentation + 1)).join(",\n")}\n${" ".repeat(indentation)}]`;
112119
}
113120
return `[${value.map((v) => serializeValue(v)).join(", ")}]`;
@@ -124,7 +131,7 @@ export function serializeValue(value: Value, indentation = 0): string {
124131
}
125132

126133
/**
127-
*
134+
* Converts a block to a string.
128135
*/
129136
export function blockToString(block: Block): string {
130137
const labels = (block.labels || []).map((l) => `"${l}"`).join(" ");

src/utils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,11 @@ export async function promptForDirectory(args: {
977977
return dir;
978978
}
979979

980-
/*
980+
/**
981981
* Deeply compares two JSON-serializable objects.
982982
* It's a simplified version of a deep equal function, sufficient for comparing the structure
983983
* of the gemini-extension.json file. It doesn't handle special cases like RegExp, Date, or functions.
984984
*/
985-
/**
986-
*
987-
*/
988985
export function deepEqual(a: any, b: any): boolean {
989986
if (a === b) {
990987
return true;
@@ -1065,6 +1062,8 @@ export function resolveWithin(base: string, subPath: string, errMsg?: string): s
10651062
*/
10661063
export function toLowerSnakeCase(s: string): string {
10671064
return s
1068-
.replace(/[A-Z]/g, (letter, index) => `${index > 0 ? "_" : ""}${letter.toLowerCase()}`)
1069-
.replace(/-/g, "_");
1065+
.replace(/([A-Z])([A-Z][a-z])/g, "$1_$2")
1066+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
1067+
.replace(/[-\s]+/g, "_")
1068+
.toLowerCase();
10701069
}

0 commit comments

Comments
 (0)