From 3f6a3cf683ad232effb54727436d0dde1522acc6 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Thu, 14 Sep 2017 14:57:19 +0200 Subject: [PATCH] Decode NUMBER into float32 and float64 fields This patch decodes a NUMBER value (e.g. "2") into a float32 and float64 field. --- decoder.go | 2 +- decoder_test.go | 9 +++++++++ test-fixtures/float.hcl | 1 + test-fixtures/float.json | 3 ++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/decoder.go b/decoder.go index 6e75ece..b88f322 100644 --- a/decoder.go +++ b/decoder.go @@ -137,7 +137,7 @@ func (d *decoder) decodeBool(name string, node ast.Node, result reflect.Value) e func (d *decoder) decodeFloat(name string, node ast.Node, result reflect.Value) error { switch n := node.(type) { case *ast.LiteralType: - if n.Token.Type == token.FLOAT { + if n.Token.Type == token.FLOAT || n.Token.Type == token.NUMBER { v, err := strconv.ParseFloat(n.Token.Text, 64) if err != nil { return err diff --git a/decoder_test.go b/decoder_test.go index 38363ad..8682f47 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -73,6 +73,7 @@ func TestDecode_interface(t *testing.T) { false, map[string]interface{}{ "a": 1.02, + "b": 2, }, }, { @@ -811,6 +812,7 @@ func TestDecode_intString(t *testing.T) { func TestDecode_float32(t *testing.T) { var value struct { A float32 `hcl:"a"` + B float32 `hcl:"b"` } err := Decode(&value, testReadFile(t, "float.hcl")) @@ -821,11 +823,15 @@ func TestDecode_float32(t *testing.T) { if got, want := value.A, float32(1.02); got != want { t.Fatalf("wrong result %#v; want %#v", got, want) } + if got, want := value.B, float32(2); got != want { + t.Fatalf("wrong result %#v; want %#v", got, want) + } } func TestDecode_float64(t *testing.T) { var value struct { A float64 `hcl:"a"` + B float64 `hcl:"b"` } err := Decode(&value, testReadFile(t, "float.hcl")) @@ -836,6 +842,9 @@ func TestDecode_float64(t *testing.T) { if got, want := value.A, float64(1.02); got != want { t.Fatalf("wrong result %#v; want %#v", got, want) } + if got, want := value.B, float64(2); got != want { + t.Fatalf("wrong result %#v; want %#v", got, want) + } } func TestDecode_intStringAliased(t *testing.T) { diff --git a/test-fixtures/float.hcl b/test-fixtures/float.hcl index eed44e5..edf355e 100644 --- a/test-fixtures/float.hcl +++ b/test-fixtures/float.hcl @@ -1 +1,2 @@ a = 1.02 +b = 2 diff --git a/test-fixtures/float.json b/test-fixtures/float.json index a9d1ab4..5808680 100644 --- a/test-fixtures/float.json +++ b/test-fixtures/float.json @@ -1,3 +1,4 @@ { - "a": 1.02 + "a": 1.02, + "b": 2 }