From 3add4592b88761509d67445bb682f224dfa79eda Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Mon, 9 Jul 2018 15:19:34 -0700 Subject: [PATCH] feat(v2): add empty visualization type and cleanup marshalling --- v2/cell.go | 38 +++++++++++++++++++++++++------------- v2/cell_test.go | 5 ++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/v2/cell.go b/v2/cell.go index 1d44be214..14383a218 100644 --- a/v2/cell.go +++ b/v2/cell.go @@ -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 diff --git a/v2/cell_test.go b/v2/cell_test.go index 87630a6ac..452143064 100644 --- a/v2/cell_test.go +++ b/v2/cell_test.go @@ -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",