sdk
sdk copied to clipboard
Cannot unmarshal object into Go struct
Hello, have an issue on GetDashboardByUID method. On some dashboards getting errors such as
2021/03/05 13:27:41 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-compute-resources-workload
2021/03/05 13:27:42 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/kubernetes-networking-namespace-pods
2021/03/05 13:27:42 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/kubernetes-networking-pod
2021/03/05 13:27:43 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/loki-quick-search
And this
2021/03/05 15:48:17 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/auth
2021/03/05 15:48:20 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-cluster
2021/03/05 15:48:30 unmarshal board: json: cannot unmarshal number into Go struct field Board.panels of type string for db/production
Additional details here
Thanks for your report! Will try looking into it by using your gist. In the mean time help is welcome :hugs:
For at least the TemplateVar.templating.list.query issue, I have fixed this in a private fork -- the problem is that prometheus based queries have a different schema, and afaict there is no way to tell what the schema is unless you know the type of the datasource (which you could only do by querying grafana during unmarshalling). I ended up writing something like the following:
func (tv *TemplateVar) UnmarshalJSON(b []byte) (err error) {
var def TemplateVarDefault
var prom TemplateVarPrometheus
if err = json.Unmarshal(b, &def); err == nil {
tv.TemplateVarDefault = &def
} else if err = json.Unmarshal(b, &prom); err == nil {
tv.TemplateVarPrometheus = &prom
} else {
return fmt.Errorf("unhandled schema")
}
return
}
Unfortunately there are a lot of places in the grafana json that require this type of approach, e.g. the Target struct which changes form in incompatible ways depending on the type of query. I'd be happy to help with some PRs, but the problem is fixes like above are breaking changes to the sdk, so I would want to get agreement on the general approach.
Hello. Any updates? This issue blocks us.
@GiedriusS this issue is still present after #151.
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-networking-workload
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-persistent-volumes
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-pods
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/kubernetes-statefulsets
2021/08/11 08:36:27 unmarshal board: json: cannot unmarshal object into Go struct field TemplateVar.templating.list.query of type string for db/loki-quick-search
Can it be reopened?
of type string for db/loki-quick-search
Isn't it interface{} now? @mpostument are you using the newest version? Could you please help me out with a test case for this?
Last tried on version v0.0.0-20210621184808-90d328319afc Example of the dashboard where it failing, also save this error on few other providers, I think on elastic search data source
Same issue, hard to debug
Hey everyone! I was wondering what the state on this one is, as it blocks to sync dashboards easily.
Looks like the cloudwatch datasource also triggers this behavior.
2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-billing
2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-cloudwatch-browser
2022/02/14 16:22:25 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-ebs
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type string for db/aws-elasticsearch
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-rds
2022/02/14 16:22:26 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-route-53
2022/02/14 16:22:27 unmarshal board: json: cannot unmarshal object into Go struct field Board.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-s3
2022/02/14 16:22:27 unmarshal board: json: cannot unmarshal object into Go struct field Row.rows.panels of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for db/aws-vpn
Looks like it's a different root cause. This issue is tracking the datasource can be a string or a map in the JSON exports. My report seems to be a different element on the Panels, unfortunately it's not as obvious as I'd like
Same here, even try the latest commit 2ff95a73e54ee3d32624e26e8880cefe7e28c8a3
json: cannot unmarshal object into Go struct field Board.panels of type string