Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dashboards: Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal #167

Merged
2 commits merged into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,52 @@ type GraphEvent struct {
}

type Yaxis struct {
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
Scale *string `json:"scale,omitempty"`
Min *float64 `json:"min,omitempty"`
AutoMin bool `json:"-"`
Max *float64 `json:"max,omitempty"`
AutoMax bool `json:"-"`
Scale *string `json:"scale,omitempty"`
}

// UnmarshalJSON is a Custom Unmarshal for Yaxis.Min/Yaxis.Max. If the datadog API
// returns "auto" for min or max, then we should set Yaxis.min or Yaxis.max to nil,
// respectively.
func (y *Yaxis) UnmarshalJSON(data []byte) error {
type Alias Yaxis
wrapper := &struct {
Min *json.Number `json:"min,omitempty"`
Max *json.Number `json:"max,omitempty"`
*Alias
}{
Alias: (*Alias)(y),
}

if err := json.Unmarshal(data, &wrapper); err != nil {
return err
}

if wrapper.Min != nil && *wrapper.Min == "auto" {
y.AutoMin = true
y.Min = nil
} else {
f, err := wrapper.Min.Float64()
if err != nil {
return err
}
y.Min = &f
}

if wrapper.Max != nil && *wrapper.Max == "auto" {
y.AutoMax = true
y.Max = nil
} else {
f, err := wrapper.Max.Float64()
if err != nil {
return err
}
y.Max = &f
}
return nil
}

type Style struct {
Expand Down
77 changes: 77 additions & 0 deletions dashboards_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package datadog

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type Alias Yaxis

type YAxisTestSuite struct {
suite.Suite
yJSON []byte
yMarshalledJSON []byte
y Yaxis
yAutoMinMaxJSON []byte
yAutoMinMaxMarshalledJSON []byte
yAutoMinMax Yaxis
}

func (suite *YAxisTestSuite) SetupTest() {
// Custom Y.Min, Y.Max
suite.yJSON = []byte(`{"min":0,"max":1,"scale":"linear"}`)
suite.yMarshalledJSON = suite.yJSON
yMinFloat := float64(0)
yMaxFloat := float64(1)
yScale := "linear"
suite.y = Yaxis{
Min: &yMinFloat,
AutoMin: false,
Max: &yMaxFloat,
AutoMax: false,
Scale: &yScale,
}
// Auto Y.Min, Y.Max
suite.yAutoMinMaxJSON = []byte(`{"min":"auto","max":"auto","scale":"linear"}`)
suite.yAutoMinMaxMarshalledJSON = []byte(`{"scale":"linear"}`)
suite.yAutoMinMax = Yaxis{
Min: nil,
AutoMin: true,
Max: nil,
AutoMax: true,
Scale: &yScale,
}
}

func (suite *YAxisTestSuite) TestYAxisJSONMarshalCustomMinMax() {
jsonData, err := json.Marshal(suite.y)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), suite.yMarshalledJSON, jsonData)
}

func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalCustomMinMax() {
var res Yaxis
err := json.Unmarshal(suite.yJSON, &res)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), suite.y, res)
}

func (suite *YAxisTestSuite) TestYAxisJSONMarshalAutoMinMax() {
jsonData, err := json.Marshal(suite.yAutoMinMax)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), suite.yAutoMinMaxMarshalledJSON, jsonData)
}

func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalAutoMinMax() {
var res Yaxis
err := json.Unmarshal(suite.yAutoMinMaxJSON, &res)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), suite.yAutoMinMax, res)
}

func TestYAxisTestSuite(t *testing.T) {
suite.Run(t, new(YAxisTestSuite))
}