-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
# terraform-type-parser- | ||
# terraform-type-parser | ||
|
||
A library to convert Terraform types to readable formats. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install terraform-type-parser | ||
``` | ||
|
||
## Usage | ||
``` | ||
import { TerraformTypeConverter } from 'terraform-type-parser'; | ||
const terraformString = "${list(object({\n open_id_url = string\n open_id_arn = string\n service_name = string\n namespace = string\n }))}"; | ||
const result = TerraformTypeConverter.parseTerraformString(terraformString); | ||
console.log(result); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "terraform-type-parser", | ||
"version": "1.0.0", | ||
"description": "A library to convert Terraform types to readable formats.", | ||
"main": "src/converter.ts", | ||
"scripts": { | ||
"test": "tsc && jest" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.0.0", | ||
"jest": "^27.0.0", | ||
"@types/jest": "^27.0.0" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { TerraformTypeConverter } from '../src/TerraformTypeConverter'; | ||
|
||
describe('TerraformTypeConverter', () => { | ||
test('converts list(object(...)) correctly', () => { | ||
const terraformString = "${list(object({\n open_id_url = string\n open_id_arn = string\n service_name = string\n namespace = string\n }))}"; | ||
const result = TerraformTypeConverter.parseTerraformString(terraformString); | ||
expect(result).toEqual({ | ||
open_id_url: 'String', | ||
open_id_arn: 'String', | ||
service_name: 'String', | ||
namespace: 'String' | ||
}); | ||
}); | ||
|
||
test('converts list(string) correctly', () => { | ||
const terraformString = "${list(string)}"; | ||
const result = TerraformTypeConverter.parseTerraformString(terraformString); | ||
expect(result).toEqual(['String']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
export class TerraformTypeConverter { | ||
private static typeMappings: Record<string, string> = { | ||
string: 'String', | ||
number: 'Number', | ||
bool: 'Bool', | ||
'list(': 'Array', | ||
'set(': 'Array', | ||
'tuple(': 'Array', | ||
'map(': 'Object', | ||
'object(': 'Object', | ||
null: 'null' | ||
}; | ||
|
||
private static extractInnerType(terraformType: string): string { | ||
const match = terraformType.match(/\((.*)\)/); | ||
if (match) { | ||
return match[1].trim(); | ||
} | ||
return 'Unknown Type'; | ||
} | ||
|
||
public static convertTerraformType(terraformType: string): string | any[] { | ||
terraformType = terraformType.trim(); | ||
|
||
for (const [key, value] of Object.entries(TerraformTypeConverter.typeMappings)) { | ||
if (terraformType.startsWith(key)) { | ||
const innerType = TerraformTypeConverter.extractInnerType(terraformType); | ||
|
||
if (value === 'Array') { | ||
const innerReadableType = TerraformTypeConverter.convertTerraformType(innerType); | ||
if (innerReadableType === 'Object') { | ||
return [{}]; | ||
} else { | ||
return [innerReadableType]; | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
|
||
if (TerraformTypeConverter.typeMappings[terraformType]) { | ||
return TerraformTypeConverter.typeMappings[terraformType]; | ||
} | ||
|
||
return 'Unknown Type'; | ||
} | ||
|
||
public static parseTerraformString(terraformString: string): any { | ||
if (terraformString.startsWith('${list')) { | ||
const innerContent = terraformString.match(/\${list\((.*)\)\}/s)?.[1]?.trim(); | ||
if (innerContent) { | ||
const parsedObject = this.parseTerraformString(`\${${innerContent}}`); | ||
return Array.isArray(parsedObject) ? parsedObject : [parsedObject]; | ||
} | ||
} | ||
|
||
const cleanString = terraformString | ||
.replace(/\${|list|set|object|\(|\)|}/g, '') | ||
.trim(); | ||
|
||
const lines = cleanString.split('\n').map(line => line.trim()).filter(Boolean); | ||
|
||
const result: Record<string, any> = {}; | ||
|
||
lines.forEach(line => { | ||
const [key, terraformType] = line.split('=').map(part => part.trim()); | ||
if (key && terraformType) { | ||
const convertedType = TerraformTypeConverter.convertTerraformType(terraformType); | ||
result[key] = convertedType; | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "commonjs", | ||
"outDir": "./dist", | ||
"strict": true | ||
}, | ||
"include": ["src/**/*.ts", "test/**/*.ts"] | ||
} |