From f178a0ce5dae8749a243a2678f71425673530a48 Mon Sep 17 00:00:00 2001 From: Jason Ernst Date: Mon, 11 Sep 2023 13:26:23 -0700 Subject: [PATCH 1/2] handle float/int slice problem and null values --- pkg/plugin/datasource.go | 62 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go index e55ab91..4ed9e22 100644 --- a/pkg/plugin/datasource.go +++ b/pkg/plugin/datasource.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "reflect" "time" "cloud.google.com/go/firestore" @@ -89,6 +90,26 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer return response } +func convertInt32To64(ar []int32) []float64 { + newar := make([]float64, len(ar)) + var v float32 + var i int + for i, v = range ar { + newar[i] = float64(v) + } + return newar +} + +func convertInt64To64(ar []int64) []float64 { + newar := make([]float64, len(ar)) + var v float32 + var i int + for i, v = range ar { + newar[i] = float64(v) + } + return newar +} + func (d *Datasource) queryInternal(ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse { var response backend.DataResponse @@ -149,24 +170,45 @@ func (d *Datasource) queryInternal(ctx context.Context, pCtx backend.PluginConte if values == nil { values = []int32{} } - values = append(values.([]int32), int32(val.(int))) + if reflect.TypeOf(values).Elem().Kind() == reflect.Float64 { + values = append(values.([]float64), val.(float64)) + } else { + values = append(values.([]int32), int32(val.(int))) + } break case int32: if values == nil { values = []int32{} } - values = append(values.([]int32), val.(int32)) + if reflect.TypeOf(values).Elem().Kind() == reflect.Float64 { + values = append(values.([]float64), val.(float64)) + } else { + values = append(values.([]int32), val.(int32)) + } break case int64: if values == nil { values = []int64{} } - values = append(values.([]int64), val.(int64)) + if reflect.TypeOf(values).Elem().Kind() == reflect.Float64 { + values = append(values.([]float64), val.(float64)) + } else { + values = append(values.([]int64), val.(int64)) + } break case float64: if values == nil { values = []float64{} } + if reflect.TypeOf(values).Elem().Kind() == reflect.Int { + values = convertInt32To64(values.([]int32)) + } + if reflect.TypeOf(values).Elem().Kind() == reflect.Int32 { + values = convertInt32To64(values.([]int32)) + } + if reflect.TypeOf(values).Elem().Kind() == reflect.Int64 { + values = convertInt64To64(values.([]int64)) + } values = append(values.([]float64), val.(float64)) break case time.Time: @@ -190,6 +232,20 @@ func (d *Datasource) queryInternal(ctx context.Context, pCtx backend.PluginConte if values == nil { values = []string{} } + if val == nil { + if reflect.TypeOf(values).Elem().Kind() == reflect.Int { + values = append(values.([]int32), 0) + } + if reflect.TypeOf(values).Elem().Kind() == reflect.Int32 { + values = append(values.([]int32), 0) + } + if reflect.TypeOf(values).Elem().Kind() == reflect.Int64 { + values = append(values.([]int64), 0) + } + if reflect.TypeOf(values).Elem().Kind() == reflect.Float64 { + values = append(values.([]float64), 0.0) + } + } values = append(values.([]string), fmt.Sprintf("%v", val)) } } From 3ebac3d58d9b6c8f864e28ca44e38e5718bdb2e4 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar <6123002+pgollangi@users.noreply.github.com> Date: Thu, 28 Sep 2023 22:08:58 +0530 Subject: [PATCH 2/2] Update datasource.go --- pkg/plugin/datasource.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go index 4ed9e22..34a768b 100644 --- a/pkg/plugin/datasource.go +++ b/pkg/plugin/datasource.go @@ -92,7 +92,7 @@ func (d *Datasource) query(ctx context.Context, pCtx backend.PluginContext, quer func convertInt32To64(ar []int32) []float64 { newar := make([]float64, len(ar)) - var v float32 + var v int32 var i int for i, v = range ar { newar[i] = float64(v) @@ -102,7 +102,7 @@ func convertInt32To64(ar []int32) []float64 { func convertInt64To64(ar []int64) []float64 { newar := make([]float64, len(ar)) - var v float32 + var v int64 var i int for i, v = range ar { newar[i] = float64(v)