Skip to content

Commit 07c6088

Browse files
authored
Chore: fix experimental QueryData map initalizers (#921)
1 parent 31dbdd2 commit 07c6088

File tree

5 files changed

+78
-20
lines changed

5 files changed

+78
-20
lines changed

data/converters/converters.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package converters
33

44
import (
5+
"encoding/json"
56
"fmt"
67
"strconv"
78
"time"
@@ -229,6 +230,10 @@ var JSONValueToFloat64 = data.FieldConverter{
229230
fV = float64(iiV)
230231
return fV, nil
231232
}
233+
nn, ok := v.(json.Number)
234+
if ok {
235+
return nn.Float64()
236+
}
232237
sV, ok := v.(string)
233238
if ok {
234239
return strconv.ParseFloat(sV, 64)
@@ -254,6 +259,14 @@ var JSONValueToInt64 = data.FieldConverter{
254259
if ok {
255260
return int64(fV), nil
256261
}
262+
ii, ok := v.(int)
263+
if ok {
264+
return int64(ii), nil
265+
}
266+
nn, ok := v.(json.Number)
267+
if ok {
268+
return nn.Int64()
269+
}
257270
return nil, toConversionError("float64", v)
258271
},
259272
}

experimental/apis/data/v0alpha1/client.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ func (c *simpleHTTPClient) QueryData(ctx context.Context, query QueryDataRequest
4646
}
4747
req.Header.Set("Content-Type", "application/json")
4848

49-
rsp, err := c.client.Do(req)
50-
if err != nil {
51-
return rsp.StatusCode, nil, err
52-
}
53-
defer rsp.Body.Close()
54-
5549
qdr := &backend.QueryDataResponse{}
56-
iter, err := jsoniter.Parse(jsoniter.ConfigCompatibleWithStandardLibrary, rsp.Body, 1024*10)
57-
if err == nil {
58-
err = iter.ReadVal(qdr)
50+
code := http.StatusNotFound // 404 for network requests etc
51+
rsp, err := c.client.Do(req)
52+
if rsp != nil {
53+
code = rsp.StatusCode
54+
defer rsp.Body.Close()
55+
if err == nil {
56+
iter, e2 := jsoniter.Parse(jsoniter.ConfigCompatibleWithStandardLibrary, rsp.Body, 1024*10)
57+
if e2 == nil {
58+
err = iter.ReadVal(qdr)
59+
}
60+
}
5961
}
60-
return rsp.StatusCode, qdr, err
62+
return code, qdr, err
6163
}

experimental/apis/data/v0alpha1/client_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
func TestClientWithBadURL(t *testing.T) {
15+
client := v0alpha1.NewQueryDataClient("http://localhostXYZ:998/api/ds/query", nil, nil)
16+
code, _, err := client.QueryData(context.Background(), v0alpha1.QueryDataRequest{})
17+
require.Error(t, err)
18+
require.Equal(t, 404, code)
19+
}
20+
1421
func TestQueryClient(t *testing.T) {
1522
t.Skip()
1623

@@ -30,7 +37,7 @@ func TestQueryClient(t *testing.T) {
3037
"uid": "PD8C576611E62080A"
3138
},
3239
"csvContent": "a,b,c\n1,hello,true",
33-
"hide": true
40+
"hide": false
3441
}
3542
]
3643
}`

experimental/apis/data/v0alpha1/query.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"unsafe"
77

8+
"github.com/grafana/grafana-plugin-sdk-go/backend"
89
"github.com/grafana/grafana-plugin-sdk-go/data"
910
"github.com/grafana/grafana-plugin-sdk-go/data/converters"
1011
"github.com/grafana/grafana-plugin-sdk-go/data/utils/jsoniter"
@@ -51,35 +52,59 @@ func (g *DataQuery) Set(key string, val any) *DataQuery {
5152
case "refId":
5253
g.RefID, _ = val.(string)
5354
case "resultAssertions":
55+
v, ok := val.(ResultAssertions)
56+
if ok {
57+
g.ResultAssertions = &v
58+
return g
59+
}
5460
body, err := json.Marshal(val)
55-
if err != nil {
56-
_ = json.Unmarshal(body, &g.ResultAssertions)
61+
if err == nil {
62+
err = json.Unmarshal(body, &g.ResultAssertions)
63+
if err != nil {
64+
backend.Logger.Warn("error reading resultAssertions from value. %w", err)
65+
}
5766
}
5867
case "timeRange":
68+
v, ok := val.(TimeRange)
69+
if ok {
70+
g.TimeRange = &v
71+
return g
72+
}
5973
body, err := json.Marshal(val)
60-
if err != nil {
61-
_ = json.Unmarshal(body, &g.TimeRange)
74+
if err == nil {
75+
err = json.Unmarshal(body, &g.TimeRange)
76+
if err != nil {
77+
backend.Logger.Warn("error reading timeRange from value. %w", err)
78+
}
6279
}
6380
case "datasource":
81+
v, ok := val.(DataSourceRef)
82+
if ok {
83+
g.Datasource = &v
84+
return g
85+
}
6486
body, err := json.Marshal(val)
65-
if err != nil {
66-
_ = json.Unmarshal(body, &g.Datasource)
87+
if err == nil {
88+
err = json.Unmarshal(body, &g.Datasource)
89+
if err != nil {
90+
backend.Logger.Warn("error reading datasource from value. %w", err)
91+
}
6792
}
6893
case "datasourceId":
6994
v, err := converters.JSONValueToInt64.Converter(val)
70-
if err != nil {
95+
if err == nil {
7196
g.DatasourceID, _ = v.(int64)
7297
}
7398
case "queryType":
7499
g.QueryType, _ = val.(string)
75100
case "maxDataPoints":
76101
v, err := converters.JSONValueToInt64.Converter(val)
77-
if err != nil {
102+
if err == nil {
78103
g.MaxDataPoints, _ = v.(int64)
79104
}
80105
case "intervalMs":
81106
v, err := converters.JSONValueToFloat64.Converter(val)
82-
if err != nil {
107+
if err == nil {
83108
g.IntervalMS, _ = v.(float64)
84109
}
85110
case "hide":

experimental/apis/data/v0alpha1/query_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,15 @@ func TestQueryBuilders(t *testing.T) {
115115
})
116116
require.Equal(t, "D", testQ3.QueryType)
117117
require.Equal(t, "E", testQ3.GetString("extra"))
118+
119+
testQ3.Set("datasource", &DataSourceRef{Type: "TYPE", UID: "UID"})
120+
require.NotNil(t, testQ3.Datasource)
121+
require.Equal(t, "TYPE", testQ3.Datasource.Type)
122+
require.Equal(t, "UID", testQ3.Datasource.UID)
123+
124+
testQ3.Set("datasource", map[string]any{"uid": "XYZ"})
125+
require.Equal(t, "XYZ", testQ3.Datasource.UID)
126+
127+
testQ3.Set("maxDataPoints", 100)
128+
require.Equal(t, int64(100), testQ3.MaxDataPoints)
118129
}

0 commit comments

Comments
 (0)