Skip to content

Commit 39b28d5

Browse files
committed
Use custom type for precision
1 parent e88e72f commit 39b28d5

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

cmd/tools/gen-accessors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
185185
zeroValue = "0"
186186
case "Status":
187187
zeroValue = "0"
188+
case "PrecisionT":
189+
zeroValue = `""`
188190
default:
189191
zeroValue = fmt.Sprintf("%s{}", x.String())
190192
}

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 *json.Number `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 *PrecisionT `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() json.Number {
2900+
func (g *GraphDefinition) GetPrecision() PrecisionT {
29012901
if g == nil || g.Precision == nil {
29022902
return ""
29032903
}
@@ -2906,7 +2906,7 @@ func (g *GraphDefinition) GetPrecision() json.Number {
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() (json.Number, bool) {
2909+
func (g *GraphDefinition) GetPrecisionOk() (PrecisionT, 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 json.Number) {
2926+
func (g *GraphDefinition) SetPrecision(v PrecisionT) {
29272927
g.Precision = &v
29282928
}
29292929

@@ -7795,7 +7795,7 @@ func (t *TileDef) SetNoMetricHosts(v bool) {
77957795
}
77967796

77977797
// GetPrecision returns the Precision field if non-nil, zero value otherwise.
7798-
func (t *TileDef) GetPrecision() json.Number {
7798+
func (t *TileDef) GetPrecision() PrecisionT {
77997799
if t == nil || t.Precision == nil {
78007800
return ""
78017801
}
@@ -7804,7 +7804,7 @@ func (t *TileDef) GetPrecision() json.Number {
78047804

78057805
// GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise
78067806
// and a boolean to check if the value has been set.
7807-
func (t *TileDef) GetPrecisionOk() (json.Number, bool) {
7807+
func (t *TileDef) GetPrecisionOk() (PrecisionT, bool) {
78087808
if t == nil || t.Precision == nil {
78097809
return "", false
78107810
}
@@ -7821,7 +7821,7 @@ func (t *TileDef) HasPrecision() bool {
78217821
}
78227822

78237823
// SetPrecision allocates a new t.Precision and returns the pointer to it.
7824-
func (t *TileDef) SetPrecision(v json.Number) {
7824+
func (t *TileDef) SetPrecision(v PrecisionT) {
78257825
t.Precision = &v
78267826
}
78277827

@@ -10089,7 +10089,7 @@ func (w *Widget) SetParams(v Params) {
1008910089
}
1009010090

1009110091
// GetPrecision returns the Precision field if non-nil, zero value otherwise.
10092-
func (w *Widget) GetPrecision() json.Number {
10092+
func (w *Widget) GetPrecision() PrecisionT {
1009310093
if w == nil || w.Precision == nil {
1009410094
return ""
1009510095
}
@@ -10098,7 +10098,7 @@ func (w *Widget) GetPrecision() json.Number {
1009810098

1009910099
// GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise
1010010100
// and a boolean to check if the value has been set.
10101-
func (w *Widget) GetPrecisionOk() (json.Number, bool) {
10101+
func (w *Widget) GetPrecisionOk() (PrecisionT, bool) {
1010210102
if w == nil || w.Precision == nil {
1010310103
return "", false
1010410104
}
@@ -10115,7 +10115,7 @@ func (w *Widget) HasPrecision() bool {
1011510115
}
1011610116

1011710117
// SetPrecision allocates a new w.Precision and returns the pointer to it.
10118-
func (w *Widget) SetPrecision(v json.Number) {
10118+
func (w *Widget) SetPrecision(v PrecisionT) {
1011910119
w.Precision = &v
1012010120
}
1012110121

helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ func GetJsonNumberOk(v *json.Number) (json.Number, bool) {
6565

6666
return "", false
6767
}
68+
69+
// Precision is a helper routine that allocates a new precision value
70+
// to store v and returns a pointer to it.
71+
func Precision(v PrecisionT) *PrecisionT { return &v }
72+
73+
// GetPrecision is a helper routine that returns a boolean representing
74+
// if a value was set, and if so, dereferences the pointer to it.
75+
func GetPrecision(v *PrecisionT) (PrecisionT, bool) {
76+
if v != nil {
77+
return *v, true
78+
}
79+
80+
return PrecisionT(""), false
81+
}

integration/screen_widgets_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestWidgets(t *testing.T) {
9191
}},
9292
CustomUnit: datadog.String("%"),
9393
Autoscale: datadog.Bool(false),
94-
Precision: datadog.JsonNumber("6"),
94+
Precision: datadog.Precision("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.JsonNumber("*"),
244+
Precision: datadog.Precision("*"),
245245
Unit: datadog.String("b"),
246246
},
247247
{

screen_widgets.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,39 @@ 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+
530
type TileDef struct {
631
Events []TileDefEvent `json:"events,omitempty"`
732
Markers []TileDefMarker `json:"markers,omitempty"`
833
Requests []TileDefRequest `json:"requests,omitempty"`
934
Viz *string `json:"viz,omitempty"`
1035
CustomUnit *string `json:"custom_unit,omitempty"`
1136
Autoscale *bool `json:"autoscale,omitempty"`
12-
Precision *json.Number `json:"precision,omitempty"`
37+
Precision *PrecisionT `json:"precision,omitempty"`
1338
TextAlign *string `json:"text_align,omitempty"`
1439

1540
// For hostmap
@@ -104,9 +129,9 @@ type Widget struct {
104129
Color *string `json:"color,omitempty"`
105130

106131
// For AlertValue widget
107-
TextSize *string `json:"text_size,omitempty"`
108-
Unit *string `json:"unit,omitempty"`
109-
Precision *json.Number `json:"precision,omitempty"`
132+
TextSize *string `json:"text_size,omitempty"`
133+
Unit *string `json:"unit,omitempty"`
134+
Precision *PrecisionT `json:"precision,omitempty"`
110135

111136
// AlertGraph widget
112137
VizType *string `json:"viz_type,omitempty"`

0 commit comments

Comments
 (0)