From e88e72fe6aad8c270f6de1f75590abf18803d21a Mon Sep 17 00:00:00 2001 From: Brendan Shaklovitz Date: Tue, 13 Nov 2018 15:08:59 -0600 Subject: [PATCH 1/2] Make Precision field in Widget *json.Number * Make Precision type for Widget match the type for TileDef. --- datadog-accessors.go | 6 +++--- integration/screen_widgets_test.go | 2 +- screen_widgets.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/datadog-accessors.go b/datadog-accessors.go index 6994935..d885838 100644 --- a/datadog-accessors.go +++ b/datadog-accessors.go @@ -10089,7 +10089,7 @@ func (w *Widget) SetParams(v Params) { } // GetPrecision returns the Precision field if non-nil, zero value otherwise. -func (w *Widget) GetPrecision() string { +func (w *Widget) GetPrecision() json.Number { if w == nil || w.Precision == nil { return "" } @@ -10098,7 +10098,7 @@ func (w *Widget) GetPrecision() string { // GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise // and a boolean to check if the value has been set. -func (w *Widget) GetPrecisionOk() (string, bool) { +func (w *Widget) GetPrecisionOk() (json.Number, bool) { if w == nil || w.Precision == nil { return "", false } @@ -10115,7 +10115,7 @@ func (w *Widget) HasPrecision() bool { } // SetPrecision allocates a new w.Precision and returns the pointer to it. -func (w *Widget) SetPrecision(v string) { +func (w *Widget) SetPrecision(v json.Number) { w.Precision = &v } diff --git a/integration/screen_widgets_test.go b/integration/screen_widgets_test.go index 5ad1beb..2c5eb3e 100644 --- a/integration/screen_widgets_test.go +++ b/integration/screen_widgets_test.go @@ -241,7 +241,7 @@ func TestWidgets(t *testing.T) { AlertID: datadog.Int(123456), TextSize: datadog.String("fill_height"), TextAlign: datadog.String("right"), - Precision: datadog.String("*"), + Precision: datadog.JsonNumber("*"), Unit: datadog.String("b"), }, { diff --git a/screen_widgets.go b/screen_widgets.go index 282e00e..2b16231 100644 --- a/screen_widgets.go +++ b/screen_widgets.go @@ -104,9 +104,9 @@ type Widget struct { Color *string `json:"color,omitempty"` // For AlertValue widget - TextSize *string `json:"text_size,omitempty"` - Unit *string `json:"unit,omitempty"` - Precision *string `json:"precision,omitempty"` + TextSize *string `json:"text_size,omitempty"` + Unit *string `json:"unit,omitempty"` + Precision *json.Number `json:"precision,omitempty"` // AlertGraph widget VizType *string `json:"viz_type,omitempty"` From 39b28d55bcc8cfc6ad67ce8562e19e3320df0e47 Mon Sep 17 00:00:00 2001 From: Brendan Shaklovitz Date: Thu, 15 Nov 2018 11:33:29 -0600 Subject: [PATCH 2/2] Use custom type for precision --- cmd/tools/gen-accessors.go | 2 ++ dashboards.go | 8 ++++---- datadog-accessors.go | 18 ++++++++-------- helpers.go | 14 +++++++++++++ integration/screen_widgets_test.go | 4 ++-- screen_widgets.go | 33 ++++++++++++++++++++++++++---- 6 files changed, 60 insertions(+), 19 deletions(-) diff --git a/cmd/tools/gen-accessors.go b/cmd/tools/gen-accessors.go index 75b7b3a..ed06d48 100644 --- a/cmd/tools/gen-accessors.go +++ b/cmd/tools/gen-accessors.go @@ -185,6 +185,8 @@ func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) { zeroValue = "0" case "Status": zeroValue = "0" + case "PrecisionT": + zeroValue = `""` default: zeroValue = fmt.Sprintf("%s{}", x.String()) } diff --git a/dashboards.go b/dashboards.go index 904eade..8c90b5c 100644 --- a/dashboards.go +++ b/dashboards.go @@ -123,10 +123,10 @@ type GraphDefinition struct { Yaxis Yaxis `json:"yaxis,omitempty"` // For query value type graphs - Autoscale *bool `json:"autoscale,omitempty"` - TextAlign *string `json:"text_align,omitempty"` - Precision *json.Number `json:"precision,omitempty"` - CustomUnit *string `json:"custom_unit,omitempty"` + Autoscale *bool `json:"autoscale,omitempty"` + TextAlign *string `json:"text_align,omitempty"` + Precision *PrecisionT `json:"precision,omitempty"` + CustomUnit *string `json:"custom_unit,omitempty"` // For hostmaps Style *Style `json:"style,omitempty"` diff --git a/datadog-accessors.go b/datadog-accessors.go index d885838..40ea61d 100644 --- a/datadog-accessors.go +++ b/datadog-accessors.go @@ -2897,7 +2897,7 @@ func (g *GraphDefinition) SetNodeType(v string) { } // GetPrecision returns the Precision field if non-nil, zero value otherwise. -func (g *GraphDefinition) GetPrecision() json.Number { +func (g *GraphDefinition) GetPrecision() PrecisionT { if g == nil || g.Precision == nil { return "" } @@ -2906,7 +2906,7 @@ func (g *GraphDefinition) GetPrecision() json.Number { // GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise // and a boolean to check if the value has been set. -func (g *GraphDefinition) GetPrecisionOk() (json.Number, bool) { +func (g *GraphDefinition) GetPrecisionOk() (PrecisionT, bool) { if g == nil || g.Precision == nil { return "", false } @@ -2923,7 +2923,7 @@ func (g *GraphDefinition) HasPrecision() bool { } // SetPrecision allocates a new g.Precision and returns the pointer to it. -func (g *GraphDefinition) SetPrecision(v json.Number) { +func (g *GraphDefinition) SetPrecision(v PrecisionT) { g.Precision = &v } @@ -7795,7 +7795,7 @@ func (t *TileDef) SetNoMetricHosts(v bool) { } // GetPrecision returns the Precision field if non-nil, zero value otherwise. -func (t *TileDef) GetPrecision() json.Number { +func (t *TileDef) GetPrecision() PrecisionT { if t == nil || t.Precision == nil { return "" } @@ -7804,7 +7804,7 @@ func (t *TileDef) GetPrecision() json.Number { // GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise // and a boolean to check if the value has been set. -func (t *TileDef) GetPrecisionOk() (json.Number, bool) { +func (t *TileDef) GetPrecisionOk() (PrecisionT, bool) { if t == nil || t.Precision == nil { return "", false } @@ -7821,7 +7821,7 @@ func (t *TileDef) HasPrecision() bool { } // SetPrecision allocates a new t.Precision and returns the pointer to it. -func (t *TileDef) SetPrecision(v json.Number) { +func (t *TileDef) SetPrecision(v PrecisionT) { t.Precision = &v } @@ -10089,7 +10089,7 @@ func (w *Widget) SetParams(v Params) { } // GetPrecision returns the Precision field if non-nil, zero value otherwise. -func (w *Widget) GetPrecision() json.Number { +func (w *Widget) GetPrecision() PrecisionT { if w == nil || w.Precision == nil { return "" } @@ -10098,7 +10098,7 @@ func (w *Widget) GetPrecision() json.Number { // GetPrecisionOk returns a tuple with the Precision field if it's non-nil, zero value otherwise // and a boolean to check if the value has been set. -func (w *Widget) GetPrecisionOk() (json.Number, bool) { +func (w *Widget) GetPrecisionOk() (PrecisionT, bool) { if w == nil || w.Precision == nil { return "", false } @@ -10115,7 +10115,7 @@ func (w *Widget) HasPrecision() bool { } // SetPrecision allocates a new w.Precision and returns the pointer to it. -func (w *Widget) SetPrecision(v json.Number) { +func (w *Widget) SetPrecision(v PrecisionT) { w.Precision = &v } diff --git a/helpers.go b/helpers.go index 866cdc5..e25d516 100644 --- a/helpers.go +++ b/helpers.go @@ -65,3 +65,17 @@ func GetJsonNumberOk(v *json.Number) (json.Number, bool) { return "", false } + +// Precision is a helper routine that allocates a new precision value +// to store v and returns a pointer to it. +func Precision(v PrecisionT) *PrecisionT { return &v } + +// GetPrecision is a helper routine that returns a boolean representing +// if a value was set, and if so, dereferences the pointer to it. +func GetPrecision(v *PrecisionT) (PrecisionT, bool) { + if v != nil { + return *v, true + } + + return PrecisionT(""), false +} diff --git a/integration/screen_widgets_test.go b/integration/screen_widgets_test.go index 2c5eb3e..4dcedd5 100644 --- a/integration/screen_widgets_test.go +++ b/integration/screen_widgets_test.go @@ -91,7 +91,7 @@ func TestWidgets(t *testing.T) { }}, CustomUnit: datadog.String("%"), Autoscale: datadog.Bool(false), - Precision: datadog.JsonNumber("6"), + Precision: datadog.Precision("6"), TextAlign: datadog.String("right"), }, }, @@ -241,7 +241,7 @@ func TestWidgets(t *testing.T) { AlertID: datadog.Int(123456), TextSize: datadog.String("fill_height"), TextAlign: datadog.String("right"), - Precision: datadog.JsonNumber("*"), + Precision: datadog.Precision("*"), Unit: datadog.String("b"), }, { diff --git a/screen_widgets.go b/screen_widgets.go index 2b16231..a09cd80 100644 --- a/screen_widgets.go +++ b/screen_widgets.go @@ -2,6 +2,31 @@ package datadog import "encoding/json" +type PrecisionT string + +// UnmarshalJSON is a Custom Unmarshal for PrecisionT. The Datadog API can +// return 1 (int), "1" (number, but a string type) or something like "100%" or +// "*" (string). +func (p *PrecisionT) UnmarshalJSON(data []byte) error { + var err error + var precisionNum json.Number + if err = json.Unmarshal(data, &precisionNum); err == nil { + *p = PrecisionT(precisionNum) + return nil + } + + var precisionStr string + if err = json.Unmarshal(data, &precisionStr); err == nil { + *p = PrecisionT(precisionStr) + return nil + } + + var p0 PrecisionT + *p = p0 + + return err +} + type TileDef struct { Events []TileDefEvent `json:"events,omitempty"` Markers []TileDefMarker `json:"markers,omitempty"` @@ -9,7 +34,7 @@ type TileDef struct { Viz *string `json:"viz,omitempty"` CustomUnit *string `json:"custom_unit,omitempty"` Autoscale *bool `json:"autoscale,omitempty"` - Precision *json.Number `json:"precision,omitempty"` + Precision *PrecisionT `json:"precision,omitempty"` TextAlign *string `json:"text_align,omitempty"` // For hostmap @@ -104,9 +129,9 @@ type Widget struct { Color *string `json:"color,omitempty"` // For AlertValue widget - TextSize *string `json:"text_size,omitempty"` - Unit *string `json:"unit,omitempty"` - Precision *json.Number `json:"precision,omitempty"` + TextSize *string `json:"text_size,omitempty"` + Unit *string `json:"unit,omitempty"` + Precision *PrecisionT `json:"precision,omitempty"` // AlertGraph widget VizType *string `json:"viz_type,omitempty"`