Skip to content

Commit

Permalink
Merge pull request #194 from nyanshak/make-widget-precision-field-jso…
Browse files Browse the repository at this point in the history
…n-number

Make Precision field in Widget *json.Number
  • Loading branch information
ojongerius authored Nov 27, 2018
2 parents 97ed528 + 39b28d5 commit 1df5bda
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
8 changes: 4 additions & 4 deletions dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
18 changes: 9 additions & 9 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -7826,7 +7826,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 ""
}
Expand All @@ -7835,7 +7835,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
}
Expand All @@ -7852,7 +7852,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
}

Expand Down Expand Up @@ -10120,7 +10120,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() PrecisionT {
if w == nil || w.Precision == nil {
return ""
}
Expand All @@ -10129,7 +10129,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() (PrecisionT, bool) {
if w == nil || w.Precision == nil {
return "", false
}
Expand All @@ -10146,7 +10146,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 PrecisionT) {
w.Precision = &v
}

Expand Down
14 changes: 14 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions integration/screen_widgets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
Expand Down Expand Up @@ -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.Precision("*"),
Unit: datadog.String("b"),
},
{
Expand Down
33 changes: 29 additions & 4 deletions screen_widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@ 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"`
Requests []TileDefRequest `json:"requests,omitempty"`
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
Expand Down Expand Up @@ -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 *string `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"`
Expand Down

0 comments on commit 1df5bda

Please sign in to comment.