json/token: fix issues with unquoting sttrings from JSON
This commit is contained in:
parent
e9e082dff4
commit
340f0af3c0
@ -14,6 +14,7 @@ type Token struct {
|
|||||||
Type Type
|
Type Type
|
||||||
Pos Pos
|
Pos Pos
|
||||||
Text string
|
Text string
|
||||||
|
JSON bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language)
|
// Type is the set of lexical tokens of the HCL (HashiCorp Configuration Language)
|
||||||
@ -138,7 +139,15 @@ func (t Token) Value() interface{} {
|
|||||||
case IDENT:
|
case IDENT:
|
||||||
return t.Text
|
return t.Text
|
||||||
case STRING:
|
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 {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("unquote %s err: %s", t.Text, err))
|
panic(fmt.Sprintf("unquote %s err: %s", t.Text, err))
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ func (p *Parser) Parse() (*ast.File, error) {
|
|||||||
|
|
||||||
// Flatten it, which finds patterns and turns them into more HCL-like
|
// Flatten it, which finds patterns and turns them into more HCL-like
|
||||||
// AST trees.
|
// AST trees.
|
||||||
flattenObjects(f.Node)
|
// flattenObjects(f.Node)
|
||||||
|
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ func (t Token) HCLToken() hcltoken.Token {
|
|||||||
case NUMBER:
|
case NUMBER:
|
||||||
return hcltoken.Token{Type: hcltoken.NUMBER, Text: t.Text}
|
return hcltoken.Token{Type: hcltoken.NUMBER, Text: t.Text}
|
||||||
case STRING:
|
case STRING:
|
||||||
return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text}
|
return hcltoken.Token{Type: hcltoken.STRING, Text: t.Text, JSON: true}
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unimplemented HCLToken for type: %s", t.Type))
|
panic(fmt.Sprintf("unimplemented HCLToken for type: %s", t.Type))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user