gokmen / hcl-to-json Goto Github PK
View Code? Open in Web Editor NEWHCL to JSON converter in CoffeeScript
License: MIT License
HCL to JSON converter in CoffeeScript
License: MIT License
Given this small example:
// This example is created to test hcl-to-json on runkit.com
hcltojson = require('hcl-to-json');
var hcl_content;
hcl_content = `
group "test" {
task "a" {
name = "A"
}
task "b" {
name = "B"
}
task "c" {
name = "C"
}
}
`
JSON.stringify(hcltojson(hcl_content), null, 2);
the following output is generated:
{
"group": {
"test": {
"task": {
"a": {
"name": "A"
},
"task": {
"b": {
"name": "B"
},
"task": {
"c": {
"name": "C"
}
}
}
}
}
}
}
I would expect it to output something like:
{
"group": {
"test": {
"task": {
"a": {
"name": "A"
},
"b": {
"name": "B"
},
"c": {
"name": "C"
}
}
}
}
}
Given a file using functions or accessing a key in a dictionary, where there are outer double quotes and inner double quotes around params, hcltojson does not properly parse the inner double quotes. Unfortunately, HCL seems not to allow single quotes at all to work around the problem.
example.tf:
resource "aws_iam_role" "child_instance_roles" {
count = "${length(split(",", var.child["instance_policy_paths"]))}"
name = "${element(split(",", var.child["instance_policy_names"]), count.index)}"
assume_role_policy = "${data.aws_iam_policy_document.ec2_assume_role.json}"
}
hcltojson example.tf
:
{
"resource": {
"aws_iam_role": {
"child_instance_roles": {
"count": "${length(split(",
", var": {
"child": {
"instance_policy_paths": {
"))}": {
"name": "${element(split(",
", var": {
"child": {
"instance_policy_names": {
"), count": {
"index)}": {
"assume_role_policy": "${data.aws_iam_policy_document.ec2_assume_role.json}"
}
}
}
}
}
}
}
}
}
}
}
}
}
Expected:
{
"resource": {
"aws_iam_role": {
"child_instance_roles": {
"count": "${length(split(\",\", var.child[\"instance_policy_paths\"]))}",
"name": "${element(split(\",\", var.child[\"instance_policy_names\"]), count.index)}",
"assume_role_policy": "${data.aws_iam_policy_document.ec2_assume_role.json}"
}
}
}
}
I receive an error when trying to parse a list of maps as a value. An example would be like the tags attribute below
resource "aws_autoscaling_group" "example" {
name = "example"
foo = "bar"
tags = [
{
key = "tag1"
value = "value1"
propagate_at_launch = true
},
{
key = "tag2"
value = "value2"
propagate_at_launch = true
},
]
}
Consider hcl as below
resource "aws_subnet" "private" {
count = "${local.private_count}"
vpc_id = "${aws_vpc.main.id}"
map_public_ip_on_launch = "false"
assign_ipv6_address_on_creation = "true"
tags = "${merge(var.tags, map("Name", "${var.name_prefix}-private-subnet-${count.index + 1}"))}"
}
Converting above file to json gives below result
{
"resource": {
"aws_subnet": {
"private": {
"count": "${local.private_count}",
"vpc_id": "${aws_vpc.main.id}",
"map_public_ip_on_launch": false,
"assign_ipv6_address_on_creation": true,
"tags": "${merge(var.tags, map(\"Name\", \"${var.name_prefix}-private-subnet-${count.index",
"+": {
"1}))}": {}
}
}
}
}
}
I tried debugging the issue and as far as i could tell the issue is after tokenize when we mark spaces then it takes into account spaces inside interpolation syntax as shown above in certain cases where it shouldn't. Possible fix is altering regex to mark spaces at https://github.com/gokmen/hcl-to-json/blob/master/src/tokenize.coffee#L42
But i am not that well versed with regexes and the impact of any change i make so thought i could have some help.
Doesn't correctly parse when there is more than one mutli-line string value. It appears to only pick up the 1st opening heredoc tag and the last closing tag. Using the below as a test example:
resource "aws_instance" "web1" {
attributes_json = <<EOF
{
"key": "value",
"app": {
"cluster1": {
"nodes": [
"webserver1",
"webserver2"
]
}
}
}
EOF
}
resource "aws_instance" "web2" {
attributes_json = <<EOF
{
"key": "value",
"app": {
"cluster2": {
"nodes": [
"webserver3",
"webserver4"
]
}
}
}
EOF
}
The above syntax gets converted to this:
{
"resource":{
"aws_instance":{
"web1":{
"attributes_json":" {\n \"key\": \"value\",\n \"app\": {\n \"cluster1\": {\n \"nodes\": [\n \"webserver1\",\n \"webserver2\"\n ]\n }\n }\n }\n EOF\n}\nresource \"aws_instance\" \"web2\" {\n attributes_json = <<EOF\n {\n \"key\": \"value\",\n \"app\": {\n \"cluster2\": {\n \"nodes\": [\n \"webserver3\",\n \"webserver4\"\n ]\n }\n }\n }\n "
},
"web2":{
"attributes_json":" {\n \"key\": \"value\",\n \"app\": {\n \"cluster1\": {\n \"nodes\": [\n \"webserver1\",\n \"webserver2\"\n ]\n }\n }\n }\n EOF\n}\nresource \"aws_instance\" \"web2\" {\n attributes_json = <<EOF\n {\n \"key\": \"value\",\n \"app\": {\n \"cluster2\": {\n \"nodes\": [\n \"webserver3\",\n \"webserver4\"\n ]\n }\n }\n }\n "
}
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.