Skip to content

Commit 77f22ba

Browse files
authored
Explain which panel could not be unmarshaled (#205)
Instead of getting an error like: unmarshal board: json: cannot unmarshal object into Go struct field Target.targets.group of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } for 000000312 AWS Cloudwatch Browser It will now mention the panel: unmarshal board: json: cannot unmarshal object into Go struct field Target.targets.group of type []struct { Type string "json:\"type,omitempty\""; Params []string "json:\"params,omitempty\"" } (panel "Metric" of type "graph") for 000000312 AWS Cloudwatch Browser Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
1 parent 13d677c commit 77f22ba

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

panel.go

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"bytes"
2424
"encoding/json"
2525
"errors"
26+
"fmt"
2627
)
2728

2829
// Each panel may be one of these types.
@@ -1030,78 +1031,85 @@ type probePanel struct {
10301031

10311032
func (p *Panel) UnmarshalJSON(b []byte) (err error) {
10321033
var probe probePanel
1033-
if err = json.Unmarshal(b, &probe); err == nil {
1034-
p.CommonPanel = probe.CommonPanel
1035-
switch probe.Type {
1036-
case "graph":
1037-
var graph GraphPanel
1038-
p.OfType = GraphType
1039-
if err = json.Unmarshal(b, &graph); err == nil {
1040-
p.GraphPanel = &graph
1041-
}
1042-
case "table":
1043-
var table TablePanel
1044-
p.OfType = TableType
1045-
if err = json.Unmarshal(b, &table); err == nil {
1046-
p.TablePanel = &table
1047-
}
1048-
case "text":
1049-
var text TextPanel
1050-
p.OfType = TextType
1051-
if err = json.Unmarshal(b, &text); err == nil {
1052-
p.TextPanel = &text
1053-
}
1054-
case "singlestat":
1055-
var singlestat SinglestatPanel
1056-
p.OfType = SinglestatType
1057-
if err = json.Unmarshal(b, &singlestat); err == nil {
1058-
p.SinglestatPanel = &singlestat
1059-
}
1060-
case "stat":
1061-
var stat StatPanel
1062-
p.OfType = StatType
1063-
if err = json.Unmarshal(b, &stat); err == nil {
1064-
p.StatPanel = &stat
1065-
}
1066-
case "dashlist":
1067-
var dashlist DashlistPanel
1068-
p.OfType = DashlistType
1069-
if err = json.Unmarshal(b, &dashlist); err == nil {
1070-
p.DashlistPanel = &dashlist
1071-
}
1072-
case "bargauge":
1073-
var bargauge BarGaugePanel
1074-
p.OfType = BarGaugeType
1075-
if err = json.Unmarshal(b, &bargauge); err == nil {
1076-
p.BarGaugePanel = &bargauge
1077-
}
1078-
case "heatmap":
1079-
var heatmap HeatmapPanel
1080-
p.OfType = HeatmapType
1081-
if err = json.Unmarshal(b, &heatmap); err == nil {
1082-
p.HeatmapPanel = &heatmap
1083-
}
1084-
case "timeseries":
1085-
var timeseries TimeseriesPanel
1086-
p.OfType = TimeseriesType
1087-
if err = json.Unmarshal(b, &timeseries); err == nil {
1088-
p.TimeseriesPanel = &timeseries
1089-
}
1090-
case "row":
1091-
var rowpanel RowPanel
1092-
p.OfType = RowType
1093-
if err = json.Unmarshal(b, &rowpanel); err == nil {
1094-
p.RowPanel = &rowpanel
1095-
}
1096-
default:
1097-
var custom = make(CustomPanel)
1098-
p.OfType = CustomType
1099-
if err = json.Unmarshal(b, &custom); err == nil {
1100-
p.CustomPanel = &custom
1101-
}
1034+
if err = json.Unmarshal(b, &probe); err != nil {
1035+
return err
1036+
}
1037+
1038+
p.CommonPanel = probe.CommonPanel
1039+
switch probe.Type {
1040+
case "graph":
1041+
var graph GraphPanel
1042+
p.OfType = GraphType
1043+
if err = json.Unmarshal(b, &graph); err == nil {
1044+
p.GraphPanel = &graph
1045+
}
1046+
case "table":
1047+
var table TablePanel
1048+
p.OfType = TableType
1049+
if err = json.Unmarshal(b, &table); err == nil {
1050+
p.TablePanel = &table
1051+
}
1052+
case "text":
1053+
var text TextPanel
1054+
p.OfType = TextType
1055+
if err = json.Unmarshal(b, &text); err == nil {
1056+
p.TextPanel = &text
1057+
}
1058+
case "singlestat":
1059+
var singlestat SinglestatPanel
1060+
p.OfType = SinglestatType
1061+
if err = json.Unmarshal(b, &singlestat); err == nil {
1062+
p.SinglestatPanel = &singlestat
1063+
}
1064+
case "stat":
1065+
var stat StatPanel
1066+
p.OfType = StatType
1067+
if err = json.Unmarshal(b, &stat); err == nil {
1068+
p.StatPanel = &stat
1069+
}
1070+
case "dashlist":
1071+
var dashlist DashlistPanel
1072+
p.OfType = DashlistType
1073+
if err = json.Unmarshal(b, &dashlist); err == nil {
1074+
p.DashlistPanel = &dashlist
1075+
}
1076+
case "bargauge":
1077+
var bargauge BarGaugePanel
1078+
p.OfType = BarGaugeType
1079+
if err = json.Unmarshal(b, &bargauge); err == nil {
1080+
p.BarGaugePanel = &bargauge
1081+
}
1082+
case "heatmap":
1083+
var heatmap HeatmapPanel
1084+
p.OfType = HeatmapType
1085+
if err = json.Unmarshal(b, &heatmap); err == nil {
1086+
p.HeatmapPanel = &heatmap
1087+
}
1088+
case "timeseries":
1089+
var timeseries TimeseriesPanel
1090+
p.OfType = TimeseriesType
1091+
if err = json.Unmarshal(b, &timeseries); err == nil {
1092+
p.TimeseriesPanel = &timeseries
1093+
}
1094+
case "row":
1095+
var rowpanel RowPanel
1096+
p.OfType = RowType
1097+
if err = json.Unmarshal(b, &rowpanel); err == nil {
1098+
p.RowPanel = &rowpanel
1099+
}
1100+
default:
1101+
var custom = make(CustomPanel)
1102+
p.OfType = CustomType
1103+
if err = json.Unmarshal(b, &custom); err == nil {
1104+
p.CustomPanel = &custom
11021105
}
11031106
}
1104-
return
1107+
1108+
if err != nil && (probe.Title != "" || probe.Type != "") {
1109+
err = fmt.Errorf("%w (panel %q of type %q)", err, probe.Title, probe.Type)
1110+
}
1111+
1112+
return err
11051113
}
11061114

11071115
func (p *Panel) MarshalJSON() ([]byte, error) {

0 commit comments

Comments
 (0)