Skip to content

Commit 21cd18a

Browse files
author
Brice Figureau
committed
Fix zorkian#192 screenboard width and height should support string
Datadog API for screenboards width and height can return either numbers , number (like “1024”) strings or even strings (like “100%”). The recent fix zorkian#190 changed those fields to be only ints, breaking the compatibility with some datadog screen boards. As suggested in zorkian#192 this change fixes the issue by making the fields become `json.Number` which should cover all possible returned values.
1 parent f3f6d2f commit 21cd18a

File tree

5 files changed

+76
-22
lines changed

5 files changed

+76
-22
lines changed

datadog-accessors.go

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

63716371
// GetHeight returns the Height field if non-nil, zero value otherwise.
6372-
func (s *Screenboard) GetHeight() int {
6372+
func (s *Screenboard) GetHeight() json.Number {
63736373
if s == nil || s.Height == nil {
6374-
return 0
6374+
return ""
63756375
}
63766376
return *s.Height
63776377
}
63786378

63796379
// GetHeightOk returns a tuple with the Height field if it's non-nil, zero value otherwise
63806380
// and a boolean to check if the value has been set.
6381-
func (s *Screenboard) GetHeightOk() (int, bool) {
6381+
func (s *Screenboard) GetHeightOk() (json.Number, bool) {
63826382
if s == nil || s.Height == nil {
6383-
return 0, false
6383+
return "", false
63846384
}
63856385
return *s.Height, true
63866386
}
@@ -6395,7 +6395,7 @@ func (s *Screenboard) HasHeight() bool {
63956395
}
63966396

63976397
// SetHeight allocates a new s.Height and returns the pointer to it.
6398-
func (s *Screenboard) SetHeight(v int) {
6398+
func (s *Screenboard) SetHeight(v json.Number) {
63996399
s.Height = &v
64006400
}
64016401

@@ -6524,18 +6524,18 @@ func (s *Screenboard) SetTitle(v string) {
65246524
}
65256525

65266526
// GetWidth returns the Width field if non-nil, zero value otherwise.
6527-
func (s *Screenboard) GetWidth() int {
6527+
func (s *Screenboard) GetWidth() json.Number {
65286528
if s == nil || s.Width == nil {
6529-
return 0
6529+
return ""
65306530
}
65316531
return *s.Width
65326532
}
65336533

65346534
// GetWidthOk returns a tuple with the Width field if it's non-nil, zero value otherwise
65356535
// and a boolean to check if the value has been set.
6536-
func (s *Screenboard) GetWidthOk() (int, bool) {
6536+
func (s *Screenboard) GetWidthOk() (json.Number, bool) {
65376537
if s == nil || s.Width == nil {
6538-
return 0, false
6538+
return "", false
65396539
}
65406540
return *s.Width, true
65416541
}
@@ -6550,7 +6550,7 @@ func (s *Screenboard) HasWidth() bool {
65506550
}
65516551

65526552
// SetWidth allocates a new s.Width and returns the pointer to it.
6553-
func (s *Screenboard) SetWidth(v int) {
6553+
func (s *Screenboard) SetWidth(v json.Number) {
65546554
s.Width = &v
65556555
}
65566556

integration/screenboards_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package integration
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/zorkian/go-datadog-api"
@@ -93,8 +94,8 @@ func TestScreenboardGet(t *testing.T) {
9394
func getTestScreenboard() *datadog.Screenboard {
9495
return &datadog.Screenboard{
9596
Title: datadog.String("___Test-Board___"),
96-
Height: datadog.Int(600),
97-
Width: datadog.Int(800),
97+
Height: datadog.JsonNumber(json.Number("600")),
98+
Width: datadog.JsonNumber(json.Number("800")),
9899
Widgets: []datadog.Widget{},
99100
}
100101
}
@@ -129,10 +130,10 @@ func assertScreenboardEquals(t *testing.T, actual, expected *datadog.Screenboard
129130
t.Errorf("Screenboard title does not match: %s != %s", *actual.Title, *expected.Title)
130131
}
131132
if *actual.Width != *expected.Width {
132-
t.Errorf("Screenboard width does not match: %d != %d", *actual.Width, *expected.Width)
133+
t.Errorf("Screenboard width does not match: %s != %s", *actual.Width, *expected.Width)
133134
}
134135
if *actual.Height != *expected.Height {
135-
t.Errorf("Screenboard width does not match: %d != %d", *actual.Height, *expected.Height)
136+
t.Errorf("Screenboard width does not match: %s != %s", *actual.Height, *expected.Height)
136137
}
137138
if len(actual.Widgets) != len(expected.Widgets) {
138139
t.Errorf("Number of Screenboard widgets does not match: %d != %d", len(actual.Widgets), len(expected.Widgets))

screenboards.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package datadog
1010

1111
import (
12+
"encoding/json"
1213
"fmt"
1314
)
1415

@@ -17,8 +18,8 @@ import (
1718
type Screenboard struct {
1819
Id *int `json:"id,omitempty"`
1920
Title *string `json:"board_title,omitempty"`
20-
Height *int `json:"height,omitempty"`
21-
Width *int `json:"width,omitempty"`
21+
Height *json.Number `json:"height,omitempty"`
22+
Width *json.Number `json:"width,omitempty"`
2223
Shared *bool `json:"shared,omitempty"`
2324
TemplateVariables []TemplateVariable `json:"template_variables,omitempty"`
2425
Widgets []Widget `json:"widgets"`

screenboards_test.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ func TestGetScreenboard(t *testing.T) {
3737
t.Fatalf("expect title %s. Got %s", expectedTitle, title)
3838
}
3939

40-
expectedHeight := 768
41-
if height := screenboard.GetHeight(); height != expectedHeight {
42-
t.Fatalf("expect height %d. Got %d", expectedHeight, height)
40+
expectedHeight := int64(768)
41+
height := screenboard.GetHeight()
42+
if h, err := height.Int64(); err != nil || h != expectedHeight {
43+
t.Fatalf("expect height %d. Got %s", expectedHeight, height)
4344
}
4445

45-
expectedWidth := 1024
46-
if width := screenboard.GetWidth(); width != expectedWidth {
47-
t.Fatalf("expect width %d. Got %d", expectedWidth, width)
46+
expectedWidth := int64(1024)
47+
width := screenboard.GetWidth()
48+
if w, err := width.Int64(); err != nil || w != expectedWidth {
49+
t.Fatalf("expect width %d. Got %s", expectedWidth, width)
4850
}
4951

5052
expectedReadOnly := false
@@ -62,6 +64,37 @@ func TestGetScreenboard(t *testing.T) {
6264
}
6365
}
6466

67+
func TestGetScreenboardWithWidhtHeightAsString(t *testing.T) {
68+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69+
response, err := ioutil.ReadFile("./tests/fixtures/screenboard_response_strings.json")
70+
if err != nil {
71+
t.Fatal(err)
72+
}
73+
w.Write(response)
74+
}))
75+
defer ts.Close()
76+
77+
datadogClient := Client{
78+
baseUrl: ts.URL,
79+
HttpClient: http.DefaultClient,
80+
}
81+
82+
screenboard, err := datadogClient.GetScreenboard(6334)
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
87+
expectedHeight := "768"
88+
if height := screenboard.GetHeight(); height.String() != expectedHeight {
89+
t.Fatalf("expect height %s. Got %s", expectedHeight, height.String())
90+
}
91+
92+
expectedWidth := "100%"
93+
if width := screenboard.GetWidth(); width.String() != expectedWidth {
94+
t.Fatalf("expect width %s. Got %s", expectedWidth, width.String())
95+
}
96+
}
97+
6598
func validateWidget(t *testing.T, wd Widget) {
6699
expectedType := "image"
67100
if widgetType := wd.GetType(); widgetType != expectedType {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"board_title": "dogapi test",
3+
"height": "768",
4+
"id": 6334,
5+
"widgets": [
6+
{
7+
"height": 20,
8+
"type": "image",
9+
"url": "http://path/to/image.jpg",
10+
"width": 32,
11+
"x": 32,
12+
"y": 7
13+
}
14+
],
15+
"width": "100%",
16+
"created": "2015-12-17T23:06:06.703087+00:00",
17+
"modified": "2015-12-17T23:12:26.726234+00:00",
18+
"read_only": false
19+
}

0 commit comments

Comments
 (0)