hcl/token: Value should unquote string

This commit is contained in:
Mitchell Hashimoto 2015-11-06 22:35:07 -08:00
parent 16ce140272
commit 68e40f3944
2 changed files with 7 additions and 2 deletions

View File

@ -136,8 +136,12 @@ func (t Token) Value() interface{} {
case IDENT: case IDENT:
return t.Text return t.Text
case STRING: case STRING:
// It is wrapped in quotes always v, err := strconv.Unquote(t.Text)
return t.Text[1 : len(t.Text)-1] if err != nil {
panic(err)
}
return v
default: default:
panic(fmt.Sprintf("unimplemented Value for type: %s", t.Type)) panic(fmt.Sprintf("unimplemented Value for type: %s", t.Type))
} }

View File

@ -49,6 +49,7 @@ func TestTokenValue(t *testing.T) {
{Token{Type: NUMBER, Text: `42`}, int64(42)}, {Token{Type: NUMBER, Text: `42`}, int64(42)},
{Token{Type: IDENT, Text: `foo`}, "foo"}, {Token{Type: IDENT, Text: `foo`}, "foo"},
{Token{Type: STRING, Text: `"foo"`}, "foo"}, {Token{Type: STRING, Text: `"foo"`}, "foo"},
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
} }
for _, token := range tokens { for _, token := range tokens {