hcl/token: Value for heredoc

This commit is contained in:
Mitchell Hashimoto 2015-11-10 14:09:01 -08:00
parent 0f965478b6
commit db97e1a99c
4 changed files with 23 additions and 16 deletions

View File

@ -226,7 +226,7 @@ func (d *decoder) decodeInterface(name string, node ast.Node, result reflect.Val
case token.NUMBER:
var result int
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case token.STRING:
case token.STRING, token.HEREDOC:
set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
default:
return fmt.Errorf(
@ -411,13 +411,13 @@ func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value)
case token.NUMBER:
result.Set(reflect.ValueOf(n.Token.Text).Convert(result.Type()))
return nil
case token.STRING:
case token.STRING, token.HEREDOC:
result.Set(reflect.ValueOf(n.Token.Value()).Convert(result.Type()))
return nil
}
}
return fmt.Errorf("%s: unknown type %T", name, node)
return fmt.Errorf("%s: unknown type for string %T", name, node)
}
func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value) error {

View File

@ -57,18 +57,16 @@ func TestDecode_interface(t *testing.T) {
"a": 1.02,
},
},
/*
{
"multiline_bad.hcl",
false,
map[string]interface{}{"foo": "bar\nbaz\n"},
},
{
"multiline.json",
false,
map[string]interface{}{"foo": "bar\nbaz"},
},
*/
{
"multiline_bad.hcl",
false,
map[string]interface{}{"foo": "bar\nbaz\n"},
},
{
"multiline.json",
false,
map[string]interface{}{"foo": "bar\nbaz"},
},
{
"scientific.json",
false,

View File

@ -5,6 +5,7 @@ package token
import (
"fmt"
"strconv"
"strings"
hclstrconv "github.com/hashicorp/hcl/hcl/strconv"
)
@ -140,6 +141,14 @@ func (t Token) Value() interface{} {
return int64(v)
case IDENT:
return t.Text
case HEREDOC:
// We need to find the end of the marker
idx := strings.IndexByte(t.Text, '\n')
if idx == -1 {
panic("heredoc doesn't contain newline")
}
return string(t.Text[idx+1 : len(t.Text)-idx+1])
case STRING:
// Determine the Unquote method to use. If it came from JSON,
// then we need to use the built-in unquote since we have to

View File

@ -33,7 +33,6 @@ func TestTypeString(t *testing.T) {
for _, token := range tokens {
if token.tt.String() != token.str {
t.Errorf("want: %q got:%q\n", token.str, token.tt)
}
}
@ -51,6 +50,7 @@ func TestTokenValue(t *testing.T) {
{Token{Type: IDENT, Text: `foo`}, "foo"},
{Token{Type: STRING, Text: `"foo"`}, "foo"},
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
{Token{Type: HEREDOC, Text: "<<EOF\nfoo\nbar\nEOF"}, "foo\nbar"},
}
for _, token := range tokens {