Skip to content

Commit 0b798a8

Browse files
authored
Fix custom Panel JSON marshalling (#167)
1 parent 9de4d14 commit 0b798a8

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

panel.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package sdk
2020
*/
2121

2222
import (
23+
"bytes"
2324
"encoding/json"
2425
"errors"
2526
)
@@ -1053,15 +1054,42 @@ func (p *Panel) MarshalJSON() ([]byte, error) {
10531054
}{p.CommonPanel, *p.HeatmapPanel}
10541055
return json.Marshal(outHeatmap)
10551056
case CustomType:
1056-
var outCustom = struct {
1057-
CommonPanel
1058-
CustomPanel
1059-
}{p.CommonPanel, *p.CustomPanel}
1057+
var outCustom = customPanelOutput{
1058+
p.CommonPanel,
1059+
*p.CustomPanel,
1060+
}
10601061
return json.Marshal(outCustom)
10611062
}
10621063
return nil, errors.New("can't marshal unknown panel type")
10631064
}
10641065

1066+
type customPanelOutput struct {
1067+
CommonPanel
1068+
CustomPanel
1069+
}
1070+
1071+
func (c customPanelOutput) MarshalJSON() ([]byte, error) {
1072+
b, err := json.Marshal(c.CommonPanel)
1073+
if err != nil {
1074+
return b, err
1075+
}
1076+
// Append custom keys to marshalled CommonPanel.
1077+
buf := bytes.NewBuffer(b[:len(b)-1])
1078+
1079+
for k, v := range c.CustomPanel {
1080+
buf.WriteString(`,"`)
1081+
buf.WriteString(k)
1082+
buf.WriteString(`":`)
1083+
b, err := json.Marshal(v)
1084+
if err != nil {
1085+
return b, err
1086+
}
1087+
buf.Write(b)
1088+
}
1089+
buf.WriteString("}")
1090+
return buf.Bytes(), nil
1091+
}
1092+
10651093
func incRefID(refID string) string {
10661094
firstLetter := refID[0]
10671095
ordinal := int(firstLetter)

panel_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,42 @@ func TestPanel_Stackdriver_ParsedTargets(t *testing.T) {
695695
t.Fatalf("should be \"pubsub.googleapis.com/subscription/ack_message_count\" but is not")
696696
}
697697
}
698+
699+
// TestCustomPanelOutput_MarshalJSON marshals new custom panel to JSON,
700+
// then marshals that json to map[string]interface{},\
701+
// and then checks both custom and non-custom keys are present and correct.
702+
func TestCustomPanelOutput_MarshalJSON(t *testing.T) {
703+
var (
704+
titleKey = "title"
705+
titleValue = "test title"
706+
customKey = "test_key"
707+
customValue = "bar_value"
708+
)
709+
p := sdk.NewCustom(titleValue)
710+
custom := map[string]interface{}(*p.CustomPanel)
711+
custom[customKey] = customValue
712+
b, err := json.Marshal(p)
713+
if err != nil {
714+
t.Fatalf("failed to marshal custom panel: %v", err)
715+
}
716+
var j = make(map[string]interface{})
717+
err = json.Unmarshal(b, &j)
718+
if err != nil {
719+
t.Fatalf("failed to unmarshal: %v", err)
720+
}
721+
val, ok := j[customKey]
722+
if !ok {
723+
t.Fatalf("failed to find key %s in map %v", customKey, j)
724+
}
725+
if val != customValue {
726+
t.Fatalf("wrong value of %s: got %s, expected %s", customKey, val, customValue)
727+
}
728+
val, ok = j[titleKey]
729+
if !ok {
730+
t.Fatalf("failed to find key %s in map %v", titleKey, j)
731+
}
732+
if val != titleValue {
733+
t.Fatalf("wrong value of %s: got %s, expected %s", titleKey, val, titleValue)
734+
}
735+
736+
}

0 commit comments

Comments
 (0)