kvz/json2hcl

json2hcl is json-esq not HCL standard

Closed this issue · 1 comments

An original tf file is like this:

resource "aws_instance" "XXX" {
  ami                         = "ami-XXXX"
  availability_zone           = "us-east-1b"
  ebs_optimized               = false
  instance_type               = "t2.micro"
  monitoring                  = false
  key_name                    = ""
  subnet_id                   = "subnet-XXXX"
  vpc_security_group_ids      = ["sg-XXX"]
  associate_public_ip_address = false
  private_ip                  = "XXXX"
  source_dest_check           = true

  root_block_device {
    volume_type           = "gp2"
    volume_size           = 8
    delete_on_termination = false
  }

  ebs_block_device {
    device_name           = "/dev/sde"
    snapshot_id           = "snap-XXXXXX"
    volume_type           = "gp2"
    volume_size           = 100
    delete_on_termination = false
  }

  tags {
    "Name" = "XXXX"
  }
}

I would expect this operation to be identical.. but it mangles it (white space as copied)

$ json2hcl --reverse < file.tf | json2hcl

"resource" = {
  "aws_instance" = {
    "" = {
      "ami" = "ami-89272XXX"

      "associate_public_ip_address" = "false"

      "availability_zone" = "us-east-1b"

      "ebs_optimized" = "false"

      "instance_type" = "t2.micro"

      "key_name" = "XXX"

      "monitoring" = "false"

      "private_ip" = "XXX"

      "root_block_device" = {
        "delete_on_termination" = "false"

        "volume_size" = 8

        "volume_type" = "gp2"
      }

      "source_dest_check" = "true"

      "subnet_id" = "subnet-XXXXX"

      "tags" = {
        "Name" = ""
      }

      "vpc_security_group_ids" = ["sg-XXXXX"]
    }
  }
}

Sadly, this behavior is caused by Hasicorp's official HCL library due to ambiguities in the HCL format. Therefore, we are not really able to improve this situation. We had similar discussions in the past, if you may want to read more on this topic and why this will probably not be changed in the future:

#3 and
hashicorp/hcl#162