diff --git a/cmd/tools/gen-accessors.go b/cmd/tools/gen-accessors.go index 172a4c3..a95c5c9 100644 --- a/cmd/tools/gen-accessors.go +++ b/cmd/tools/gen-accessors.go @@ -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()) } diff --git a/common.go b/common.go index 25e4d57..60a534b 100644 --- a/common.go +++ b/common.go @@ -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 @@ -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 +} diff --git a/datadog-accessors.go b/datadog-accessors.go index 5692e44..d9a9efd 100644 --- a/datadog-accessors.go +++ b/datadog-accessors.go @@ -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 "" } @@ -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 } @@ -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 } diff --git a/helpers.go b/helpers.go index ddd2cb5..3cfd184 100644 --- a/helpers.go +++ b/helpers.go @@ -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 +} diff --git a/integration/screen_widgets_test.go b/integration/screen_widgets_test.go index 1b9aff3..bf85c15 100644 --- a/integration/screen_widgets_test.go +++ b/integration/screen_widgets_test.go @@ -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"), }}, diff --git a/screen_widgets.go b/screen_widgets.go index 4c0872f..2990298 100644 --- a/screen_widgets.go +++ b/screen_widgets.go @@ -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 { diff --git a/screenboards_test.go b/screenboards_test.go index 8593e69..830a4a2 100644 --- a/screenboards_test.go +++ b/screenboards_test.go @@ -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) { diff --git a/testdata/fixtures/screenboard_response_marker_label.json b/testdata/fixtures/screenboard_response_marker_label.json new file mode 100644 index 0000000..120ff15 --- /dev/null +++ b/testdata/fixtures/screenboard_response_marker_label.json @@ -0,0 +1,14 @@ +{ + "widgets": [ + { + "tile_def": { + "markers": [ + { + "label": true + } + ] + }, + "type": "timeseries" + } + ] +}