Skip to content

Commit

Permalink
dashboards: Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal (#167
Browse files Browse the repository at this point in the history
)

* Add Yaxis.AutoMin, YAxis.AutoMax, and custom Unmarshal
* Add in the AutoMin and AutoMax fields for the Yaxis struct so we can work around the times when datadog's API returns a string when we expect a float64.
* Add a custom UnmarshalJSON for Yaxis to utilize the new AutoMin and AutoMax fields.
* Update custom Yaxis UnmarshalJSON and add unittests
  • Loading branch information
anthony-tsang authored and yfronto committed Aug 24, 2018
1 parent d7b8b10 commit 47fbc01
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
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))
}

0 comments on commit 47fbc01

Please sign in to comment.