Skip to content

Commit 61e10ed

Browse files
committed
Accepts ints and strings for Screenboard size.
Besides integer sizes in pixel (e.g. 768) the API can also return percentages (e.g. "100%"). Fixes zorkian#192
1 parent 3d26c69 commit 61e10ed

File tree

5 files changed

+96
-66
lines changed

5 files changed

+96
-66
lines changed

datadog-accessors.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6400,18 +6400,18 @@ func (r *Rule) SetTimeframe(v string) {
64006400
}
64016401

64026402
// GetHeight returns the Height field if non-nil, zero value otherwise.
6403-
func (s *Screenboard) GetHeight() int {
6403+
func (s *Screenboard) GetHeight() StrIntD {
64046404
if s == nil || s.Height == nil {
6405-
return 0
6405+
return ""
64066406
}
64076407
return *s.Height
64086408
}
64096409

64106410
// GetHeightOk returns a tuple with the Height field if it's non-nil, zero value otherwise
64116411
// and a boolean to check if the value has been set.
6412-
func (s *Screenboard) GetHeightOk() (int, bool) {
6412+
func (s *Screenboard) GetHeightOk() (StrIntD, bool) {
64136413
if s == nil || s.Height == nil {
6414-
return 0, false
6414+
return "", false
64156415
}
64166416
return *s.Height, true
64176417
}
@@ -6426,7 +6426,7 @@ func (s *Screenboard) HasHeight() bool {
64266426
}
64276427

64286428
// SetHeight allocates a new s.Height and returns the pointer to it.
6429-
func (s *Screenboard) SetHeight(v int) {
6429+
func (s *Screenboard) SetHeight(v StrIntD) {
64306430
s.Height = &v
64316431
}
64326432

@@ -6555,18 +6555,18 @@ func (s *Screenboard) SetTitle(v string) {
65556555
}
65566556

65576557
// GetWidth returns the Width field if non-nil, zero value otherwise.
6558-
func (s *Screenboard) GetWidth() int {
6558+
func (s *Screenboard) GetWidth() StrIntD {
65596559
if s == nil || s.Width == nil {
6560-
return 0
6560+
return ""
65616561
}
65626562
return *s.Width
65636563
}
65646564

65656565
// GetWidthOk returns a tuple with the Width field if it's non-nil, zero value otherwise
65666566
// and a boolean to check if the value has been set.
6567-
func (s *Screenboard) GetWidthOk() (int, bool) {
6567+
func (s *Screenboard) GetWidthOk() (StrIntD, bool) {
65686568
if s == nil || s.Width == nil {
6569-
return 0, false
6569+
return "", false
65706570
}
65716571
return *s.Width, true
65726572
}
@@ -6581,7 +6581,7 @@ func (s *Screenboard) HasWidth() bool {
65816581
}
65826582

65836583
// SetWidth allocates a new s.Width and returns the pointer to it.
6584-
func (s *Screenboard) SetWidth(v int) {
6584+
func (s *Screenboard) SetWidth(v StrIntD) {
65856585
s.Width = &v
65866586
}
65876587

integration/screenboards_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package integration
33
import (
44
"testing"
55

6-
"github.com/zorkian/go-datadog-api"
6+
datadog "github.com/zorkian/go-datadog-api"
77
)
88

99
func TestScreenboardCreateAndDelete(t *testing.T) {
@@ -93,8 +93,8 @@ func TestScreenboardGet(t *testing.T) {
9393
func getTestScreenboard() *datadog.Screenboard {
9494
return &datadog.Screenboard{
9595
Title: datadog.String("___Test-Board___"),
96-
Height: datadog.Int(600),
97-
Width: datadog.Int(800),
96+
Height: datadog.StrInt("600"),
97+
Width: datadog.StrInt("800"),
9898
Widgets: []datadog.Widget{},
9999
}
100100
}
@@ -129,10 +129,10 @@ func assertScreenboardEquals(t *testing.T, actual, expected *datadog.Screenboard
129129
t.Errorf("Screenboard title does not match: %s != %s", *actual.Title, *expected.Title)
130130
}
131131
if *actual.Width != *expected.Width {
132-
t.Errorf("Screenboard width does not match: %d != %d", *actual.Width, *expected.Width)
132+
t.Errorf("Screenboard width does not match: %s != %s", *actual.Width, *expected.Width)
133133
}
134134
if *actual.Height != *expected.Height {
135-
t.Errorf("Screenboard width does not match: %d != %d", *actual.Height, *expected.Height)
135+
t.Errorf("Screenboard width does not match: %s != %s", *actual.Height, *expected.Height)
136136
}
137137
if len(actual.Widgets) != len(expected.Widgets) {
138138
t.Errorf("Number of Screenboard widgets does not match: %d != %d", len(actual.Widgets), len(expected.Widgets))

screenboards.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
type Screenboard struct {
1818
Id *int `json:"id,omitempty"`
1919
Title *string `json:"board_title,omitempty"`
20-
Height *int `json:"height,omitempty"`
21-
Width *int `json:"width,omitempty"`
20+
Height *StrIntD `json:"height,omitempty"`
21+
Width *StrIntD `json:"width,omitempty"`
2222
Shared *bool `json:"shared,omitempty"`
2323
TemplateVariables []TemplateVariable `json:"template_variables,omitempty"`
2424
Widgets []Widget `json:"widgets"`

screenboards_test.go

Lines changed: 70 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,81 @@ import (
1212
)
1313

1414
func TestGetScreenboard(t *testing.T) {
15-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16-
response, err := ioutil.ReadFile("./testdata/fixtures/screenboard_response.json")
17-
if err != nil {
18-
// mustn't call t.Fatal from a different Goroutine
19-
http.Error(w, err.Error(), http.StatusInternalServerError)
20-
return
21-
}
22-
w.Write(response)
23-
}))
24-
defer ts.Close()
25-
26-
datadogClient := Client{
27-
baseUrl: ts.URL,
28-
HttpClient: ts.Client(),
29-
RetryTimeout: 5 * time.Second,
30-
}
31-
32-
got, err := datadogClient.GetScreenboard(6334)
33-
if err != nil {
34-
t.Fatal(err)
35-
}
36-
37-
want := &Screenboard{
38-
Id: Int(6334),
39-
Title: String("dogapi test"),
40-
Height: Int(768),
41-
Width: Int(1024),
42-
ReadOnly: Bool(false),
43-
Widgets: []Widget{
44-
{
45-
Type: String("image"),
46-
Height: Int(20),
47-
Width: Int(32),
48-
X: Int(32),
49-
Y: Int(7),
50-
URL: String("http://path/to/image.jpg"),
51-
},
52-
{
53-
Type: String("timeseries"),
54-
TileDef: &TileDef{
55-
Precision: StrInt("42"),
15+
tests := []struct {
16+
file string
17+
want *Screenboard
18+
}{
19+
{
20+
file: "screenboard_response",
21+
want: &Screenboard{
22+
Id: Int(6334),
23+
Title: String("dogapi test"),
24+
Height: StrInt("768"),
25+
Width: StrInt("1024"),
26+
ReadOnly: Bool(false),
27+
Widgets: []Widget{
28+
{
29+
Type: String("image"),
30+
Height: Int(20),
31+
Width: Int(32),
32+
X: Int(32),
33+
Y: Int(7),
34+
URL: String("http://path/to/image.jpg"),
35+
},
36+
{
37+
Type: String("timeseries"),
38+
TileDef: &TileDef{
39+
Precision: StrInt("42"),
40+
},
41+
},
42+
{
43+
Type: String("timeseries"),
44+
TileDef: &TileDef{
45+
Precision: StrInt("*"),
46+
},
47+
},
5648
},
5749
},
58-
{
59-
Type: String("timeseries"),
60-
TileDef: &TileDef{
61-
Precision: StrInt("*"),
62-
},
50+
},
51+
{
52+
file: "screenboard_response_fullscreen",
53+
want: &Screenboard{
54+
Id: Int(6335),
55+
Title: String("dogapi fullscreen test"),
56+
Height: StrInt("100%"),
57+
Width: StrInt("100%"),
58+
ReadOnly: Bool(false),
6359
},
6460
},
6561
}
62+
for _, tt := range tests {
63+
t.Run(tt.file, func(t *testing.T) {
64+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65+
response, err := ioutil.ReadFile("./testdata/fixtures/" + tt.file + ".json")
66+
if err != nil {
67+
// mustn't call t.Fatal from a different Goroutine
68+
http.Error(w, err.Error(), http.StatusInternalServerError)
69+
return
70+
}
71+
w.Write(response)
72+
}))
73+
defer ts.Close()
74+
75+
datadogClient := Client{
76+
baseUrl: ts.URL,
77+
HttpClient: ts.Client(),
78+
RetryTimeout: 5 * time.Second,
79+
}
80+
81+
got, err := datadogClient.GetScreenboard(6334)
82+
if err != nil {
83+
t.Fatal(err)
84+
}
6685

67-
if !reflect.DeepEqual(got, want) {
68-
// Go formatting doesn't handle pointers gracefully, so we're spewing
69-
t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(got), spew.Sdump(want))
86+
if !reflect.DeepEqual(got, tt.want) {
87+
// Go formatting doesn't handle pointers gracefully, so we're spewing
88+
t.Errorf("got:\n%s\nwant:\n%s", spew.Sdump(got), spew.Sdump(tt.want))
89+
}
90+
})
7091
}
7192
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"board_title": "dogapi fullscreen test",
3+
"id": 6335,
4+
"height": "100%",
5+
"width": "100%",
6+
"created": "2019-03-01T13:37:00.420042+01:00",
7+
"modified": "2019-03-01T15:25:26.726234+01:00",
8+
"read_only": false
9+
}

0 commit comments

Comments
 (0)