Skip to content
This repository was archived by the owner on Apr 21, 2019. It is now read-only.

Issues converting to json when using terraform functions inside interpolation #11

Open
vinaynb opened this issue Jun 26, 2018 · 3 comments

Comments

@vinaynb
Copy link

vinaynb commented Jun 26, 2018

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.

@vinaynb
Copy link
Author

vinaynb commented Jun 27, 2018

@gokmen any help ?

@gokmen
Copy link
Owner

gokmen commented Jul 2, 2018

Hi @vinaynb I've looked at this, unfortunately there is no easy fix for it. I need to change whole implementation to be able to handle quoted strings in quotes correctly but I don't have time for it for now. Patches are welcome, if you need help for fixing it I would like to help. Feel free to try it out with existing test suite by running npm run test after changing. Or while changing if you want to run tests automatically you can use npm run test:watch If you add your test case to the test file and the expected result you can check it out that one directly. For debugging if you need https://github.com/gokmen/hcl-to-json/blob/master/README.md#development

@vinaynb
Copy link
Author

vinaynb commented Jul 10, 2018

I have a patch in my mind but i am not too confident that it handles all cases well but nevertheless i'll try it out with the test suite and check. Thank you for the help !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants