Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions data/converters/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package converters

import (
"encoding/json"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -229,6 +230,14 @@ var JSONValueToFloat64 = data.FieldConverter{
fV = float64(iiV)
return fV, nil
}
ii, ok := v.(int)
if ok {
return float64(ii), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already covered in the lines just above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dooh -- thanks

nn, ok := v.(json.Number)
if ok {
return nn.Float64()
}
sV, ok := v.(string)
if ok {
return strconv.ParseFloat(sV, 64)
Expand All @@ -254,6 +263,14 @@ var JSONValueToInt64 = data.FieldConverter{
if ok {
return int64(fV), nil
}
ii, ok := v.(int)
if ok {
return int64(ii), nil
}
nn, ok := v.(json.Number)
if ok {
return nn.Int64()
}
return nil, toConversionError("float64", v)
},
}
Expand Down
12 changes: 6 additions & 6 deletions experimental/apis/data/v0alpha1/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,34 @@ func (g *DataQuery) Set(key string, val any) *DataQuery {
g.RefID, _ = val.(string)
case "resultAssertions":
body, err := json.Marshal(val)
if err != nil {
if err == nil {
_ = json.Unmarshal(body, &g.ResultAssertions)
}
case "timeRange":
body, err := json.Marshal(val)
if err != nil {
if err == nil {
_ = json.Unmarshal(body, &g.TimeRange)
}
case "datasource":
body, err := json.Marshal(val)
if err != nil {
if err == nil {
_ = json.Unmarshal(body, &g.Datasource)
Copy link
Member

@toddtreece toddtreece Mar 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these unmarshal errors be handled/logged?

}
case "datasourceId":
v, err := converters.JSONValueToInt64.Converter(val)
if err != nil {
if err == nil {
g.DatasourceID, _ = v.(int64)
}
case "queryType":
g.QueryType, _ = val.(string)
case "maxDataPoints":
v, err := converters.JSONValueToInt64.Converter(val)
if err != nil {
if err == nil {
g.MaxDataPoints, _ = v.(int64)
}
case "intervalMs":
v, err := converters.JSONValueToFloat64.Converter(val)
if err != nil {
if err == nil {
g.IntervalMS, _ = v.(float64)
}
case "hide":
Expand Down
11 changes: 11 additions & 0 deletions experimental/apis/data/v0alpha1/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,15 @@ func TestQueryBuilders(t *testing.T) {
})
require.Equal(t, "D", testQ3.QueryType)
require.Equal(t, "E", testQ3.GetString("extra"))

testQ3.Set("datasource", &DataSourceRef{Type: "TYPE", UID: "UID"})
require.NotNil(t, testQ3.Datasource)
require.Equal(t, "TYPE", testQ3.Datasource.Type)
require.Equal(t, "UID", testQ3.Datasource.UID)

testQ3.Set("datasource", map[string]any{"uid": "XYZ"})
require.Equal(t, "XYZ", testQ3.Datasource.UID)

testQ3.Set("maxDataPoints", 100)
require.Equal(t, int64(100), testQ3.MaxDataPoints)
}