Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tqindia authored Sep 10, 2024
1 parent 4e4d77a commit e0e6b84
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
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);
```
15 changes: 15 additions & 0 deletions package.json
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"
}
}

20 changes: 20 additions & 0 deletions src/converter.test.ts
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']);
});
});
77 changes: 77 additions & 0 deletions src/converter.ts
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;
}
}

9 changes: 9 additions & 0 deletions tsconfig.json
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"]
}

0 comments on commit e0e6b84

Please sign in to comment.