Skip to content

Commit

Permalink
Fix custom panel marshalling to be idempotent (grafana-tools#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba authored and wurbanski committed Sep 4, 2023
1 parent 4f5ede2 commit afbe2cc
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
)

// Each panel may be one of these types.
Expand Down Expand Up @@ -1219,11 +1220,18 @@ func (c customPanelOutput) MarshalJSON() ([]byte, error) {
// Append custom keys to marshalled CommonPanel.
buf := bytes.NewBuffer(b[:len(b)-1])

for k, v := range c.CustomPanel {
// Sort keys to make output idempotent
keys := make([]string, 0, len(c.CustomPanel))
for k := range c.CustomPanel {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
buf.WriteString(`,"`)
buf.WriteString(k)
buf.WriteString(`":`)
b, err := json.Marshal(v)
b, err := json.Marshal(c.CustomPanel[k])
if err != nil {
return b, err
}
Expand Down

0 comments on commit afbe2cc

Please sign in to comment.