From 478854d36d5b43959729ae99f7db36d33e221de6 Mon Sep 17 00:00:00 2001 From: Roman Khavronenko Date: Mon, 18 Oct 2021 15:10:44 +0300 Subject: [PATCH] vmctl: follow-up after 95d1d38595861f3ec5a8aae07df31c699e8c41ee (#1718) Signed-off-by: hagen1778 --- app/vmctl/influx/parser.go | 3 +-- app/vmctl/influx/parser_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/vmctl/influx/parser.go b/app/vmctl/influx/parser.go index 5cd5a374c3..a4b7ab7d6a 100644 --- a/app/vmctl/influx/parser.go +++ b/app/vmctl/influx/parser.go @@ -61,9 +61,8 @@ func toFloat64(v interface{}) (float64, error) { case bool: if i { return 1, nil - } else { - return 0, nil } + return 0, nil default: return 0, fmt.Errorf("unexpected value type %v", i) } diff --git a/app/vmctl/influx/parser_test.go b/app/vmctl/influx/parser_test.go index 15ce9adaa5..70ee424bba 100644 --- a/app/vmctl/influx/parser_test.go +++ b/app/vmctl/influx/parser_test.go @@ -1,6 +1,7 @@ package influx import ( + "encoding/json" "reflect" "testing" ) @@ -58,3 +59,28 @@ func TestSeries_Unmarshal(t *testing.T) { } } } + +func TestToFloat64(t *testing.T) { + f := func(in interface{}, want float64) { + t.Helper() + got, err := toFloat64(in) + if err != nil { + t.Fatalf("unexpected err: %s", err) + } + if got != want { + t.Errorf("got %v; want %v", got, want) + } + } + f("123.4", 123.4) + f(float64(123.4), 123.4) + f(float32(12), 12) + f(123, 123) + f(true, 1) + f(false, 0) + f(json.Number("123456.789"), 123456.789) + + _, err := toFloat64("text") + if err == nil { + t.Fatalf("expected to get err; got nil instead") + } +}