Skip to content

Commit

Permalink
Merge pull request hashicorp#277 from hashicorp/decode-string-booleans
Browse files Browse the repository at this point in the history
Decode string booleans
  • Loading branch information
langmartin committed Jun 11, 2019
2 parents 99e2f22 + 1804807 commit cf7d376
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (d *decoder) decodeString(name string, node ast.Node, result reflect.Value)
switch n := node.(type) {
case *ast.LiteralType:
switch n.Token.Type {
case token.NUMBER:
case token.NUMBER, token.FLOAT, token.BOOL:
result.Set(reflect.ValueOf(n.Token.Text).Convert(result.Type()))
return nil
case token.STRING, token.HEREDOC:
Expand Down
22 changes: 22 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,28 @@ func TestDecode_float64(t *testing.T) {
}
}

func TestDecode_string(t *testing.T) {
type value struct {
A string `hcl:"a"`
B string `hcl:"b"`
C string `hcl:"c"`
D string `hcl:"d"`
E string `hcl:"e"`
}

got := value{}
err := Decode(&got, testReadFile(t, "string.hcl"))
if err != nil {
t.Fatal(err)
}

want := value{"s", "2", "2.718", "true", "false"}
if !reflect.DeepEqual(want, got) {
t.Fatalf("expected %#v; got %#v", want, got)
}

}

func TestDecode_intStringAliased(t *testing.T) {
var value struct {
Count time.Duration
Expand Down
5 changes: 5 additions & 0 deletions test-fixtures/string.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a = "s"
b = 2
c = 2.718
d = true
e = false

0 comments on commit cf7d376

Please sign in to comment.