From 340f0af3c0f95770e6ffddb0db0e665907321f72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 8 Nov 2015 16:08:36 -0800 Subject: [PATCH] json/token: fix issues with unquoting sttrings from JSON --- hcl/token/token.go | 11 ++++++++++- json/parser/parser.go | 2 +- json/token/token.go | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/hcl/token/token.go b/hcl/token/token.go index e8f9635..c93f077 100644 --- a/hcl/token/token.go +++ b/hcl/token/token.go @@ -14,6 +14,7 @@ type Token struct { Type Type Pos Pos Text string + JSON bool } // Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language) @@ -138,7 +139,15 @@ func (t Token) Value() interface{} { case IDENT: return t.Text case STRING: - v, err := hclstrconv.Unquote(t.Text) + // Determine the Unquote method to use. If it came from JSON, + // then we need to use the built-in unquote since we have to + // escape interpolations there. + f := hclstrconv.Unquote + if t.JSON { + f = strconv.Unquote + } + + v, err := f(t.Text) if err != nil { panic(fmt.Sprintf("unquote %s err: %s", t.Text, err)) } diff --git a/json/parser/parser.go b/json/parser/parser.go index 19895a4..450b875 100644 --- a/json/parser/parser.go +++ b/json/parser/parser.go @@ -57,7 +57,7 @@ func (p *Parser) Parse() (*ast.File, error) { // Flatten it, which finds patterns and turns them into more HCL-like // AST trees. - flattenObjects(f.Node) + // flattenObjects(f.Node) return f, nil } diff --git a/json/token/token.go b/json/token/token.go index 96c8f67..95a0c3e 100644 --- a/json/token/token.go +++ b/json/token/token.go @@ -111,7 +111,7 @@ func (t Token) HCLToken() hcltoken.Token { case NUMBER: return hcltoken.Token{Type: hcltoken.NUMBER, Text: t.Text} case STRING: - return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text} + return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text, JSON: true} default: panic(fmt.Sprintf("unimplemented HCLToken for type: %s", t.Type)) }