@@ -16,6 +16,7 @@ import (
16
16
func init () { //nolint:gochecknoinits
17
17
jsoniter .RegisterTypeEncoder ("v0alpha1.DataQuery" , & genericQueryCodec {})
18
18
jsoniter .RegisterTypeDecoder ("v0alpha1.DataQuery" , & genericQueryCodec {})
19
+ jsoniter .RegisterTypeDecoder ("v0alpha1.DataSourceRef" , & datasourceRefCodec {})
19
20
}
20
21
21
22
type QueryDataRequest struct {
@@ -157,6 +158,7 @@ func (g *DataQuery) GetString(key string) string {
157
158
}
158
159
159
160
type genericQueryCodec struct {}
161
+ type datasourceRefCodec struct {}
160
162
161
163
func (codec * genericQueryCodec ) IsEmpty (_ unsafe.Pointer ) bool {
162
164
return false
@@ -180,6 +182,33 @@ func (codec *genericQueryCodec) Decode(ptr unsafe.Pointer, iter *j.Iterator) {
180
182
* ((* DataQuery )(ptr )) = q
181
183
}
182
184
185
+ // Long ago dashboards referenced data sources with only the name
186
+ func (codec * datasourceRefCodec ) Decode (ptr unsafe.Pointer , iter * j.Iterator ) {
187
+ q := DataSourceRef {}
188
+ switch iter .WhatIsNext () {
189
+ case j .StringValue :
190
+ q .UID = iter .ReadString ()
191
+ case j .ObjectValue :
192
+ for field := iter .ReadObject (); field != "" ; field = iter .ReadObject () {
193
+ if iter .Error != nil {
194
+ return
195
+ }
196
+ switch field {
197
+ case "type" :
198
+ q .Type = iter .ReadString ()
199
+ case "uid" :
200
+ q .UID = iter .ReadString ()
201
+ default :
202
+ _ = iter .Read () // ignore unused properties
203
+ }
204
+ }
205
+ default :
206
+ iter .Error = fmt .Errorf ("expected string or object" )
207
+ return
208
+ }
209
+ * ((* DataSourceRef )(ptr )) = q
210
+ }
211
+
183
212
// MarshalJSON writes JSON including the common and custom values
184
213
func (g DataQuery ) MarshalJSON () ([]byte , error ) {
185
214
cfg := j .ConfigCompatibleWithStandardLibrary
@@ -199,6 +228,15 @@ func (g *DataQuery) UnmarshalJSON(b []byte) error {
199
228
return g .readQuery (iter )
200
229
}
201
230
231
+ // UnmarshalJSON reads a query from json byte array
232
+ func (g * DataSourceRef ) UnmarshalJSON (b []byte ) error {
233
+ iter , err := jsoniter .ParseBytes (jsoniter .ConfigDefault , b )
234
+ if err != nil {
235
+ return err
236
+ }
237
+ return iter .Unmarshal (b , g )
238
+ }
239
+
202
240
func (g * DataQuery ) DeepCopyInto (out * DataQuery ) {
203
241
* out = * g
204
242
g .CommonQueryProperties .DeepCopyInto (& out .CommonQueryProperties )
@@ -305,19 +343,7 @@ func (g *CommonQueryProperties) readQuery(iter *jsoniter.Iterator,
305
343
err = iter .ReadVal (& g .TimeRange )
306
344
case "datasource" :
307
345
// Old datasource values may just be a string
308
- next , err = iter .WhatIsNext ()
309
- if err != nil {
310
- return err
311
- }
312
- switch next {
313
- case j .StringValue :
314
- g .Datasource = & DataSourceRef {}
315
- g .Datasource .UID , err = iter .ReadString ()
316
- case j .ObjectValue :
317
- err = iter .ReadVal (& g .Datasource )
318
- default :
319
- return fmt .Errorf ("expected string or object" )
320
- }
346
+ err = iter .ReadVal (& g .Datasource )
321
347
322
348
case "datasourceId" :
323
349
g .DatasourceID , err = iter .ReadInt64 ()
0 commit comments