From 1f63d5ffd63196856f3a737dbe459337a35280a7 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Mon, 10 Jun 2019 12:13:18 -0400 Subject: [PATCH 1/2] decoder on string decode, accept floating point numbers and booleans --- decoder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decoder.go b/decoder.go index 5e32563..6ed2fb9 100644 --- a/decoder.go +++ b/decoder.go @@ -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: From 1804807358d86424faacbb42f50f0c04303cef11 Mon Sep 17 00:00:00 2001 From: Lang Martin Date: Mon, 10 Jun 2019 12:16:27 -0400 Subject: [PATCH 2/2] decoder test string decode --- decoder_test.go | 22 ++++++++++++++++++++++ test-fixtures/string.hcl | 5 +++++ 2 files changed, 27 insertions(+) create mode 100644 test-fixtures/string.hcl diff --git a/decoder_test.go b/decoder_test.go index 70da72d..4de8168 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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 diff --git a/test-fixtures/string.hcl b/test-fixtures/string.hcl new file mode 100644 index 0000000..6071418 --- /dev/null +++ b/test-fixtures/string.hcl @@ -0,0 +1,5 @@ +a = "s" +b = 2 +c = 2.718 +d = true +e = false