Skip to content

Commit

Permalink
Accept bools and strings as Marker Labels
Browse files Browse the repository at this point in the history
This introduces a new type StrBoolD (similar to StrIntD) for types that can be
either strings or bools in JSON.

Fixes zorkian#220
  • Loading branch information
mrwonko committed Mar 4, 2019
1 parent 4c4edea commit 25df54e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cmd/tools/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
zeroValue = "0"
case "StrIntD":
zeroValue = `""`
case "StrBoolD":
zeroValue = `""`
default:
zeroValue = fmt.Sprintf("%s{}", x.String())
}
Expand Down
30 changes: 29 additions & 1 deletion common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package datadog

import "encoding/json"
import (
"encoding/json"
"strconv"
)

// StrIntD can unmarshal both number and string JSON values.
type StrIntD string
Expand All @@ -19,3 +22,28 @@ func (s *StrIntD) UnmarshalJSON(data []byte) error {
*s = ""
return err
}

// StrBoolD can unmarshal both boolean and string JSON values.
type StrBoolD string

// UnmarshalJSON is a Custom Unmarshal for StrBoolD. The Datadog API can
// return true (boolean), "true" (boolean, but as a string type) or entirely
// different strings like "test marker".
func (s *StrBoolD) UnmarshalJSON(data []byte) error {
var b bool
err := json.Unmarshal(data, &b)
if err == nil {
*s = StrBoolD(strconv.FormatBool(b))
return nil
}

var str string
err = json.Unmarshal(data, &str)
if err == nil {
*s = StrBoolD(str)
return nil
}

*s = ""
return err
}
6 changes: 3 additions & 3 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7981,7 +7981,7 @@ func (t *TileDefEvent) SetQuery(v string) {
}

// GetLabel returns the Label field if non-nil, zero value otherwise.
func (t *TileDefMarker) GetLabel() string {
func (t *TileDefMarker) GetLabel() StrBoolD {
if t == nil || t.Label == nil {
return ""
}
Expand All @@ -7990,7 +7990,7 @@ func (t *TileDefMarker) GetLabel() string {

// GetLabelOk returns a tuple with the Label field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (t *TileDefMarker) GetLabelOk() (string, bool) {
func (t *TileDefMarker) GetLabelOk() (StrBoolD, bool) {
if t == nil || t.Label == nil {
return "", false
}
Expand All @@ -8007,7 +8007,7 @@ func (t *TileDefMarker) HasLabel() bool {
}

// SetLabel allocates a new t.Label and returns the pointer to it.
func (t *TileDefMarker) SetLabel(v string) {
func (t *TileDefMarker) SetLabel(v StrBoolD) {
t.Label = &v
}

Expand Down
14 changes: 14 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ func GetStrInt(v *StrIntD) (StrIntD, bool) {

return StrIntD(""), false
}

// StrBool is a helper routine that allocates a new StrBool value
// to store v and returns a pointer to it.
func StrBool(v StrBoolD) *StrBoolD { return &v }

// GetStrBool is a helper routine that returns a boolean representing
// if a value was set, and if so, dereferences the pointer to it.
func GetStrBool(v *StrBoolD) (StrBoolD, bool) {
if v != nil {
return *v, true
}

return StrBoolD(""), false
}
2 changes: 1 addition & 1 deletion integration/screen_widgets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestWidgets(t *testing.T) {
},
}},
Markers: []datadog.TileDefMarker{{
Label: datadog.String("test marker"),
Label: datadog.StrBool("test marker"),
Type: datadog.String("error dashed"),
Value: datadog.String("y < 6"),
}},
Expand Down
6 changes: 3 additions & 3 deletions screen_widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type TileDefEvent struct {
}

type TileDefMarker struct {
Label *string `json:"label,omitempty"`
Type *string `json:"type,omitempty"`
Value *string `json:"value,omitempty"`
Label *StrBoolD `json:"label,omitempty"`
Type *string `json:"type,omitempty"`
Value *string `json:"value,omitempty"`
}

type TileDefRequest struct {
Expand Down
17 changes: 17 additions & 0 deletions screenboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ func TestGetScreenboard(t *testing.T) {
},
},
},
{
file: "screenboard_response_marker_label",
want: &Screenboard{
Widgets: []Widget{
{
Type: String("timeseries"),
TileDef: &TileDef{
Markers: []TileDefMarker{
{
Label: StrBool("true"),
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions testdata/fixtures/screenboard_response_marker_label.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"widgets": [
{
"tile_def": {
"markers": [
{
"label": true
}
]
},
"type": "timeseries"
}
]
}

0 comments on commit 25df54e

Please sign in to comment.