feat(v2): add empty visualization type and cleanup marshalling
parent
0c00a9ec3f
commit
3add4592b8
38
v2/cell.go
38
v2/cell.go
|
@ -42,6 +42,7 @@ type CellContentsUpdate struct {
|
||||||
|
|
||||||
// CellFilter represents a set of filter that restrict the returned results.
|
// CellFilter represents a set of filter that restrict the returned results.
|
||||||
type CellFilter struct {
|
type CellFilter struct {
|
||||||
|
ID *ID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cell holds positional and visual information for a cell.
|
// Cell holds positional and visual information for a cell.
|
||||||
|
@ -59,6 +60,11 @@ type Visualization interface {
|
||||||
Visualization()
|
Visualization()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EmptyVisualization is visuaization that has no values
|
||||||
|
type EmptyVisualization struct{}
|
||||||
|
|
||||||
|
func (v EmptyVisualization) Visualization() {}
|
||||||
|
|
||||||
func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
|
func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
|
||||||
var v struct {
|
var v struct {
|
||||||
B json.RawMessage `json:"visualization"`
|
B json.RawMessage `json:"visualization"`
|
||||||
|
@ -68,6 +74,11 @@ func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
|
||||||
return nil, err
|
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 {
|
var t struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
@ -84,6 +95,12 @@ func UnmarshalVisualizationJSON(b []byte) (Visualization, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
vis = qv
|
vis = qv
|
||||||
|
case "empty":
|
||||||
|
var ev EmptyVisualization
|
||||||
|
if err := json.Unmarshal(v.B, &ev); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vis = ev
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown type %v", t.Type)
|
return nil, fmt.Errorf("unknown type %v", t.Type)
|
||||||
}
|
}
|
||||||
|
@ -102,6 +119,14 @@ func MarshalVisualizationJSON(v Visualization) ([]byte, error) {
|
||||||
Type: "chronograf-v1",
|
Type: "chronograf-v1",
|
||||||
V1Visualization: vis,
|
V1Visualization: vis,
|
||||||
}
|
}
|
||||||
|
case EmptyVisualization:
|
||||||
|
s = struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
EmptyVisualization
|
||||||
|
}{
|
||||||
|
Type: "empty",
|
||||||
|
EmptyVisualization: vis,
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported type")
|
return nil, fmt.Errorf("unsupported type")
|
||||||
}
|
}
|
||||||
|
@ -141,19 +166,6 @@ func (u *CellUpdate) UnmarshalJSON(b []byte) error {
|
||||||
return err
|
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)
|
v, err := UnmarshalVisualizationJSON(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/influxdata/chronograf/v2"
|
"github.com/influxdata/chronograf/v2"
|
||||||
"github.com/influxdata/platform"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCell_MarshalJSON(t *testing.T) {
|
func TestCell_MarshalJSON(t *testing.T) {
|
||||||
|
@ -25,7 +24,7 @@ func TestCell_MarshalJSON(t *testing.T) {
|
||||||
args: args{
|
args: args{
|
||||||
cell: chronograf.Cell{
|
cell: chronograf.Cell{
|
||||||
CellContents: chronograf.CellContents{
|
CellContents: chronograf.CellContents{
|
||||||
ID: platform.ID("0"), // This ends up being id 30 encoded
|
ID: chronograf.ID("0"),
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
},
|
},
|
||||||
Visualization: chronograf.V1Visualization{
|
Visualization: chronograf.V1Visualization{
|
||||||
|
@ -36,7 +35,7 @@ func TestCell_MarshalJSON(t *testing.T) {
|
||||||
wants: wants{
|
wants: wants{
|
||||||
json: `
|
json: `
|
||||||
{
|
{
|
||||||
"id": "30",
|
"id": "0",
|
||||||
"name": "hello",
|
"name": "hello",
|
||||||
"visualization": {
|
"visualization": {
|
||||||
"type": "chronograf-v1",
|
"type": "chronograf-v1",
|
||||||
|
|
Loading…
Reference in New Issue