Skip to content

Commit aa0be81

Browse files
teraken0509mrwonko
authored andcommitted
Rename PrecisionT to StrintD and add tests
We'll use that type for other things besides precision.
1 parent 768c3f0 commit aa0be81

File tree

9 files changed

+71
-51
lines changed

9 files changed

+71
-51
lines changed

cmd/tools/gen-accessors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
185185
zeroValue = "0"
186186
case "Status":
187187
zeroValue = "0"
188-
case "PrecisionT":
188+
case "StrintD":
189189
zeroValue = `""`
190190
default:
191191
zeroValue = fmt.Sprintf("%s{}", x.String())

common.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package datadog
2+
3+
import "encoding/json"
4+
5+
// StrintD can unmarshal both number and string JSON values.
6+
type StrintD string
7+
8+
// UnmarshalJSON is a Custom Unmarshal for StrintD. The Datadog API can
9+
// return 1 (int), "1" (number, but a string type) or something like "100%" or
10+
// "*" (string).
11+
func (s *StrintD) UnmarshalJSON(data []byte) error {
12+
var num json.Number // json.Number will happily accept any string
13+
err := json.Unmarshal(data, &num)
14+
if err == nil {
15+
*s = StrintD(num.String())
16+
return nil
17+
}
18+
19+
*s = ""
20+
return err
21+
}

dashboards.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ type GraphDefinition struct {
123123
Yaxis Yaxis `json:"yaxis,omitempty"`
124124

125125
// For query value type graphs
126-
Autoscale *bool `json:"autoscale,omitempty"`
127-
TextAlign *string `json:"text_align,omitempty"`
128-
Precision *PrecisionT `json:"precision,omitempty"`
129-
CustomUnit *string `json:"custom_unit,omitempty"`
126+
Autoscale *bool `json:"autoscale,omitempty"`
127+
TextAlign *string `json:"text_align,omitempty"`
128+
Precision *StrintD `json:"precision,omitempty"`
129+
CustomUnit *string `json:"custom_unit,omitempty"`
130130

131131
// For hostmaps
132132
Style *Style `json:"style,omitempty"`

datadog-accessors.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,7 @@ func (g *GraphDefinition) SetNodeType(v string) {
28972897
}
28982898

28992899
// GetPrecision returns the Precision field if non-nil, zero value otherwise.
2900-
func (g *GraphDefinition) GetPrecision() PrecisionT {
2900+
func (g *GraphDefinition) GetPrecision() StrintD {
29012901
if g == nil || g.Precision == nil {
29022902
return ""
29032903
}
@@ -2906,7 +2906,7 @@ func (g *GraphDefinition) GetPrecision() PrecisionT {
29062906

29072907
// GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise
29082908
// and a boolean to check if the value has been set.
2909-
func (g *GraphDefinition) GetPrecisionOk() (PrecisionT, bool) {
2909+
func (g *GraphDefinition) GetPrecisionOk() (StrintD, bool) {
29102910
if g == nil || g.Precision == nil {
29112911
return "", false
29122912
}
@@ -2923,7 +2923,7 @@ func (g *GraphDefinition) HasPrecision() bool {
29232923
}
29242924

29252925
// SetPrecision allocates a new g.Precision and returns the pointer to it.
2926-
func (g *GraphDefinition) SetPrecision(v PrecisionT) {
2926+
func (g *GraphDefinition) SetPrecision(v StrintD) {
29272927
g.Precision = &v
29282928
}
29292929

@@ -7826,7 +7826,7 @@ func (t *TileDef) SetNoMetricHosts(v bool) {
78267826
}
78277827

78287828
// GetPrecision returns the Precision field if non-nil, zero value otherwise.
7829-
func (t *TileDef) GetPrecision() PrecisionT {
7829+
func (t *TileDef) GetPrecision() StrintD {
78307830
if t == nil || t.Precision == nil {
78317831
return ""
78327832
}
@@ -7835,7 +7835,7 @@ func (t *TileDef) GetPrecision() PrecisionT {
78357835

78367836
// GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise
78377837
// and a boolean to check if the value has been set.
7838-
func (t *TileDef) GetPrecisionOk() (PrecisionT, bool) {
7838+
func (t *TileDef) GetPrecisionOk() (StrintD, bool) {
78397839
if t == nil || t.Precision == nil {
78407840
return "", false
78417841
}
@@ -7852,7 +7852,7 @@ func (t *TileDef) HasPrecision() bool {
78527852
}
78537853

78547854
// SetPrecision allocates a new t.Precision and returns the pointer to it.
7855-
func (t *TileDef) SetPrecision(v PrecisionT) {
7855+
func (t *TileDef) SetPrecision(v StrintD) {
78567856
t.Precision = &v
78577857
}
78587858

@@ -10120,7 +10120,7 @@ func (w *Widget) SetParams(v Params) {
1012010120
}
1012110121

1012210122
// GetPrecision returns the Precision field if non-nil, zero value otherwise.
10123-
func (w *Widget) GetPrecision() PrecisionT {
10123+
func (w *Widget) GetPrecision() StrintD {
1012410124
if w == nil || w.Precision == nil {
1012510125
return ""
1012610126
}
@@ -10129,7 +10129,7 @@ func (w *Widget) GetPrecision() PrecisionT {
1012910129

1013010130
// GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise
1013110131
// and a boolean to check if the value has been set.
10132-
func (w *Widget) GetPrecisionOk() (PrecisionT, bool) {
10132+
func (w *Widget) GetPrecisionOk() (StrintD, bool) {
1013310133
if w == nil || w.Precision == nil {
1013410134
return "", false
1013510135
}
@@ -10146,7 +10146,7 @@ func (w *Widget) HasPrecision() bool {
1014610146
}
1014710147

1014810148
// SetPrecision allocates a new w.Precision and returns the pointer to it.
10149-
func (w *Widget) SetPrecision(v PrecisionT) {
10149+
func (w *Widget) SetPrecision(v StrintD) {
1015010150
w.Precision = &v
1015110151
}
1015210152

helpers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ func GetJsonNumberOk(v *json.Number) (json.Number, bool) {
6666
return "", false
6767
}
6868

69-
// Precision is a helper routine that allocates a new precision value
69+
// Strint is a helper routine that allocates a new strint value
7070
// to store v and returns a pointer to it.
71-
func Precision(v PrecisionT) *PrecisionT { return &v }
71+
func Strint(v StrintD) *StrintD { return &v }
7272

73-
// GetPrecision is a helper routine that returns a boolean representing
73+
// GetStrint is a helper routine that returns a boolean representing
7474
// if a value was set, and if so, dereferences the pointer to it.
75-
func GetPrecision(v *PrecisionT) (PrecisionT, bool) {
75+
func GetStrint(v *StrintD) (StrintD, bool) {
7676
if v != nil {
7777
return *v, true
7878
}
7979

80-
return PrecisionT(""), false
80+
return StrintD(""), false
8181
}

integration/screen_widgets_test.go

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

66
"github.com/stretchr/testify/assert"
7-
"github.com/zorkian/go-datadog-api"
7+
datadog "github.com/zorkian/go-datadog-api"
88
)
99

1010
func TestWidgets(t *testing.T) {
@@ -91,7 +91,7 @@ func TestWidgets(t *testing.T) {
9191
}},
9292
CustomUnit: datadog.String("%"),
9393
Autoscale: datadog.Bool(false),
94-
Precision: datadog.Precision("6"),
94+
Precision: datadog.Strint("6"),
9595
TextAlign: datadog.String("right"),
9696
},
9797
},
@@ -241,7 +241,7 @@ func TestWidgets(t *testing.T) {
241241
AlertID: datadog.Int(123456),
242242
TextSize: datadog.String("fill_height"),
243243
TextAlign: datadog.String("right"),
244-
Precision: datadog.Precision("*"),
244+
Precision: datadog.Strint("*"),
245245
Unit: datadog.String("b"),
246246
},
247247
{

screen_widgets.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,14 @@ package datadog
22

33
import "encoding/json"
44

5-
type PrecisionT string
6-
7-
// UnmarshalJSON is a Custom Unmarshal for PrecisionT. The Datadog API can
8-
// return 1 (int), "1" (number, but a string type) or something like "100%" or
9-
// "*" (string).
10-
func (p *PrecisionT) UnmarshalJSON(data []byte) error {
11-
var err error
12-
var precisionNum json.Number
13-
if err = json.Unmarshal(data, &precisionNum); err == nil {
14-
*p = PrecisionT(precisionNum)
15-
return nil
16-
}
17-
18-
var precisionStr string
19-
if err = json.Unmarshal(data, &precisionStr); err == nil {
20-
*p = PrecisionT(precisionStr)
21-
return nil
22-
}
23-
24-
var p0 PrecisionT
25-
*p = p0
26-
27-
return err
28-
}
29-
305
type TileDef struct {
316
Events []TileDefEvent `json:"events,omitempty"`
327
Markers []TileDefMarker `json:"markers,omitempty"`
338
Requests []TileDefRequest `json:"requests,omitempty"`
349
Viz *string `json:"viz,omitempty"`
3510
CustomUnit *string `json:"custom_unit,omitempty"`
3611
Autoscale *bool `json:"autoscale,omitempty"`
37-
Precision *PrecisionT `json:"precision,omitempty"`
12+
Precision *StrintD `json:"precision,omitempty"`
3813
TextAlign *string `json:"text_align,omitempty"`
3914

4015
// For hostmap
@@ -129,9 +104,9 @@ type Widget struct {
129104
Color *string `json:"color,omitempty"`
130105

131106
// For AlertValue widget
132-
TextSize *string `json:"text_size,omitempty"`
133-
Unit *string `json:"unit,omitempty"`
134-
Precision *PrecisionT `json:"precision,omitempty"`
107+
TextSize *string `json:"text_size,omitempty"`
108+
Unit *string `json:"unit,omitempty"`
109+
Precision *StrintD `json:"precision,omitempty"`
135110

136111
// AlertGraph widget
137112
VizType *string `json:"viz_type,omitempty"`

screenboards_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ func TestGetScreenboard(t *testing.T) {
4848
Y: Int(7),
4949
URL: String("http://path/to/image.jpg"),
5050
},
51+
{
52+
Type: String("timeseries"),
53+
TileDef: &TileDef{
54+
Precision: Strint("42"),
55+
},
56+
},
57+
{
58+
Type: String("timeseries"),
59+
TileDef: &TileDef{
60+
Precision: Strint("*"),
61+
},
62+
},
5163
},
5264
}
5365

testdata/fixtures/screenboard_response.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
"width": 32,
1111
"x": 32,
1212
"y": 7
13+
},
14+
{
15+
"type": "timeseries",
16+
"tile_def": {
17+
"precision": 42
18+
}
19+
},
20+
{
21+
"type": "timeseries",
22+
"tile_def": {
23+
"precision": "*"
24+
}
1325
}
1426
],
1527
"width": 1024,

0 commit comments

Comments
 (0)