Skip to content

Commit cf7d376

Browse files
authored
Merge pull request hashicorp#277 from hashicorp/decode-string-booleans
Decode string booleans
2 parents 99e2f22 + 1804807 commit cf7d376

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

decoder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value)
535535
switch n := node.(type) {
536536
case *ast.LiteralType:
537537
switch n.Token.Type {
538-
case token.NUMBER:
538+
case token.NUMBER, token.FLOAT, token.BOOL:
539539
result.Set(reflect.ValueOf(n.Token.Text).Convert(result.Type()))
540540
return nil
541541
case token.STRING, token.HEREDOC:

decoder_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,28 @@ func TestDecode_float64(t *testing.T) {
951951
}
952952
}
953953

954+
func TestDecode_string(t *testing.T) {
955+
type value struct {
956+
A string `hcl:"a"`
957+
B string `hcl:"b"`
958+
C string `hcl:"c"`
959+
D string `hcl:"d"`
960+
E string `hcl:"e"`
961+
}
962+
963+
got := value{}
964+
err := Decode(&got, testReadFile(t, "string.hcl"))
965+
if err != nil {
966+
t.Fatal(err)
967+
}
968+
969+
want := value{"s", "2", "2.718", "true", "false"}
970+
if !reflect.DeepEqual(want, got) {
971+
t.Fatalf("expected %#v; got %#v", want, got)
972+
}
973+
974+
}
975+
954976
func TestDecode_intStringAliased(t *testing.T) {
955977
var value struct {
956978
Count time.Duration

test-fixtures/string.hcl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = "s"
2+
b = 2
3+
c = 2.718
4+
d = true
5+
e = false

0 commit comments

Comments
 (0)