Parsing interpolated string from JSON
- Dominant language
- Go
- Stars
- 5.8k
- Forks
- 657
- Avg merge
- 20h 36m
- Merged PRs (30d)
- 6
Description
I maintain a tool for converting JSON to HCL and vice versa (to a certain extend): https://github.com/kvz/json2hcl. Some users have reported problems converting the JSON to HCL if a string interpolation contains quotes.
Basically, if a string, such as `"${format(var.bamboo_url, \"PRJ-KEY\", var.skip_branches ? \"true\" : \"false\")}"` is parsed using the JSON parser and then printed/formatted using the HCL printer, the blackslashes in front of the quotes are kept: `"${format(var.bamboo_url, \"PRJ-KEY\", var.skip_branches ? \"true\" : \"false\")}"` (same output as input) . However, as far as I know they should be removed.
I am not sure if this is a problem with the JSON parser or HCL printer or if this is even intended.
Below you can find a Go program which demonstrates the problem:
### Example Template
```go
package main
import (
"fmt"
"os"
"github.com/hashicorp/hcl/hcl/printer"
jsonParser "github.com/hashicorp/hcl/json/parser"
)
func main() {
err := toHCL()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func toHCL() error {
input := `{ "url":"${format(var.bamboo_url, \"PRJ-KEY\", var.skip_branches ? \"true\" : \"false\")}" }`
ast, err := jsonParser.Parse([]byte(input))
if err != nil {
return fmt.Errorf("unable to parse JSON: %s", err)
}
err = printer.Fprint(os.Stdout, ast)
if err != nil {
return fmt.Errorf("unable to print HCL: %s", err)
}
return nil
}
```
### Expected behavior
The program should print:
```
"url" = "${format(var.bamboo_url, "PRJ-KEY", var.skip_branches ? "true" : "false")}"
```
### Actual behavior
The program prints:
```
"url" = "${format(var.bamboo_url, \"PRJ-KEY\", var.skip_branches ? \"true\" : \"false\")}"
```
### Steps to reproduce
1. Save the example as `main.go`
2. `go get .`
3. `go run main.go`
### References
Are there any other GitHub issues (open or closed) that should
be linked here? For example:
- https://github.com/kvz/json2hcl/issues/7
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the main.go reproduction, following jsonParser.Parse and printer.Fprint to determine where the escaped quotes are retained. Run the example with go run main.go and compare the output with the expected HCL string; done means the interpolation's inner quotes are printed without the extra backslashes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, json
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100