json/token: fix issues with unquoting sttrings from JSON

This commit is contained in:
Mitchell Hashimoto 2015-11-08 16:08:36 -08:00
parent e9e082dff4
commit 340f0af3c0
3 changed files with 12 additions and 3 deletions

View File

@ -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))
}

View File

@ -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
}

View File

@ -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))
}