@@ -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 */
1717export 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 */
4749export 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 */
6367export 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 */
8690export 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 */
96103export 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 */
129136export function blockToString ( block : Block ) : string {
130137 const labels = ( block . labels || [ ] ) . map ( ( l ) => `"${ l } "` ) . join ( " " ) ;
0 commit comments