feat(v2): add empty visualization type and cleanup marshalling

pull/3842/head
Michael Desa 2018-07-09 15:19:34 -07:00
parent 0c00a9ec3f
commit 3add4592b8
2 changed files with 27 additions and 16 deletions

View File

@ -42,6 +42,7 @@ type CellContentsUpdate struct {
// CellFilter represents a set of filter that restrict the returned results.
type CellFilter struct {
ID *ID
}
// Cell holds positional and visual information for a cell.
@ -59,6 +60,11 @@ type Visualization interface {
Visualization()
}
// EmptyVisualization is visuaization that has no values
type EmptyVisualization struct{}
func (v EmptyVisualization) Visualization() {}
func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
var v struct {
B json.RawMessage `json:"visualization"`
@ -68,6 +74,11 @@ func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
return nil, err
}
if len(v.B) == 0 {
// Then there wasn't any visualizaiton field, so there's no need unmarshal it
return EmptyVisualization{}, nil
}
var t struct {
Type string `json:"type"`
}
@ -84,6 +95,12 @@ func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
return nil, err
}
vis = qv
case "empty":
var ev EmptyVisualization
if err := json.Unmarshal(v.B, &ev); err != nil {
return nil, err
}
vis = ev
default:
return nil, fmt.Errorf("unknown type %v", t.Type)
}
@ -102,6 +119,14 @@ func MarshalVisualizationJSON(v Visualization) ([]byte, error) {
Type: "chronograf-v1",
V1Visualization: vis,
}
case EmptyVisualization:
s = struct {
Type string `json:"type"`
EmptyVisualization
}{
Type: "empty",
EmptyVisualization: vis,
}
default:
return nil, fmt.Errorf("unsupported type")
}
@ -141,19 +166,6 @@ func (u *CellUpdate) UnmarshalJSON(b []byte) error {
return err
}
var vs struct {
B json.RawMessage `json:"visualization"`
}
if err := json.Unmarshal(b, &vs); err != nil {
return err
}
if len(vs.B) == 0 {
// Then there wasn't any visualizaiton field, so there's no need to update it
return nil
}
v, err := UnmarshalVisualizationJSON(b)
if err != nil {
return err

View File

@ -6,7 +6,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/influxdata/chronograf/v2"
"github.com/influxdata/platform"
)
func TestCell_MarshalJSON(t *testing.T) {
@ -25,7 +24,7 @@ func TestCell_MarshalJSON(t *testing.T) {
args: args{
cell: chronograf.Cell{
CellContents: chronograf.CellContents{
ID: platform.ID("0"), // This ends up being id 30 encoded
ID: chronograf.ID("0"),
Name: "hello",
},
Visualization: chronograf.V1Visualization{
@ -36,7 +35,7 @@ func TestCell_MarshalJSON(t *testing.T) {
wants: wants{
json: `
{
"id": "30",
"id": "0",
"name": "hello",
"visualization": {
"type": "chronograf-v1",