hcl/token: Value for heredoc
This commit is contained in:
parent
0f965478b6
commit
db97e1a99c
@ -226,7 +226,7 @@ func (d *decoder) decodeInterface(name string, node ast.Node, result reflect.Val
|
|||||||
case token.NUMBER:
|
case token.NUMBER:
|
||||||
var result int
|
var result int
|
||||||
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
|
set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
|
||||||
case token.STRING:
|
case token.STRING, token.HEREDOC:
|
||||||
set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
|
set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
@ -411,13 +411,13 @@ func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value)
|
|||||||
case token.NUMBER:
|
case token.NUMBER:
|
||||||
result.Set(reflect.ValueOf(n.Token.Text).Convert(result.Type()))
|
result.Set(reflect.ValueOf(n.Token.Text).Convert(result.Type()))
|
||||||
return nil
|
return nil
|
||||||
case token.STRING:
|
case token.STRING, token.HEREDOC:
|
||||||
result.Set(reflect.ValueOf(n.Token.Value()).Convert(result.Type()))
|
result.Set(reflect.ValueOf(n.Token.Value()).Convert(result.Type()))
|
||||||
return nil
|
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 {
|
func (d *decoder) decodeStruct(name string, node ast.Node, result reflect.Value) error {
|
||||||
|
@ -57,18 +57,16 @@ func TestDecode_interface(t *testing.T) {
|
|||||||
"a": 1.02,
|
"a": 1.02,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
/*
|
{
|
||||||
{
|
"multiline_bad.hcl",
|
||||||
"multiline_bad.hcl",
|
false,
|
||||||
false,
|
map[string]interface{}{"foo": "bar\nbaz\n"},
|
||||||
map[string]interface{}{"foo": "bar\nbaz\n"},
|
},
|
||||||
},
|
{
|
||||||
{
|
"multiline.json",
|
||||||
"multiline.json",
|
false,
|
||||||
false,
|
map[string]interface{}{"foo": "bar\nbaz"},
|
||||||
map[string]interface{}{"foo": "bar\nbaz"},
|
},
|
||||||
},
|
|
||||||
*/
|
|
||||||
{
|
{
|
||||||
"scientific.json",
|
"scientific.json",
|
||||||
false,
|
false,
|
||||||
|
@ -5,6 +5,7 @@ package token
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
hclstrconv "github.com/hashicorp/hcl/hcl/strconv"
|
hclstrconv "github.com/hashicorp/hcl/hcl/strconv"
|
||||||
)
|
)
|
||||||
@ -140,6 +141,14 @@ func (t Token) Value() interface{} {
|
|||||||
return int64(v)
|
return int64(v)
|
||||||
case IDENT:
|
case IDENT:
|
||||||
return t.Text
|
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:
|
case STRING:
|
||||||
// Determine the Unquote method to use. If it came from JSON,
|
// Determine the Unquote method to use. If it came from JSON,
|
||||||
// then we need to use the built-in unquote since we have to
|
// then we need to use the built-in unquote since we have to
|
||||||
|
@ -33,7 +33,6 @@ func TestTypeString(t *testing.T) {
|
|||||||
for _, token := range tokens {
|
for _, token := range tokens {
|
||||||
if token.tt.String() != token.str {
|
if token.tt.String() != token.str {
|
||||||
t.Errorf("want: %q got:%q\n", token.str, token.tt)
|
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: IDENT, Text: `foo`}, "foo"},
|
||||||
{Token{Type: STRING, Text: `"foo"`}, "foo"},
|
{Token{Type: STRING, Text: `"foo"`}, "foo"},
|
||||||
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
|
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
|
||||||
|
{Token{Type: HEREDOC, Text: "<<EOF\nfoo\nbar\nEOF"}, "foo\nbar"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, token := range tokens {
|
for _, token := range tokens {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user