From e9bfdbfac6cd77af709311c69276dab3dc43809e Mon Sep 17 00:00:00 2001 From: Michel Blanc Date: Sat, 3 Nov 2018 12:09:23 +0100 Subject: [PATCH] Fixes inconsistent API typing for sub_type UptimeRobot API returns inconsistent types on sub_type (just like keyword_type). This is addressed using interface{} instead of int --- pkg/testdata/getMonitors.json | 17 ++++++++++++++++- pkg/uptimerobot.go | 12 ++++++------ pkg/uptimerobot_test.go | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/testdata/getMonitors.json b/pkg/testdata/getMonitors.json index acaef33..94ca12e 100644 --- a/pkg/testdata/getMonitors.json +++ b/pkg/testdata/getMonitors.json @@ -3,7 +3,7 @@ "pagination": { "offset": 0, "limit": 50, - "total": 2 + "total": 3 }, "monitors": [ { @@ -73,6 +73,21 @@ "duration": 22 } ] + }, + { + "id": 777559666, + "friendly_name": "My FTP Server", + "url": "ftp.mywebpage.com", + "type": 4, + "sub_type": 3, + "keyword_type": null, + "keyword_value": "", + "http_username": "", + "http_password": "", + "port": 21, + "interval": 60, + "status": 2, + "create_datetime": 0 } ] } \ No newline at end of file diff --git a/pkg/uptimerobot.go b/pkg/uptimerobot.go index d5939b2..7c0f425 100644 --- a/pkg/uptimerobot.go +++ b/pkg/uptimerobot.go @@ -97,12 +97,12 @@ func (a AlertContact) String() string { // Monitor represents an UptimeRobot monitor. type Monitor struct { - ID int64 `json:"id"` - FriendlyName string `json:"friendly_name"` - URL string `json:"url"` - Type int `json:"type"` - SubType string `json:"sub_type"` - // keyword_type is returned as either an integer or an empty string, + ID int64 `json:"id"` + FriendlyName string `json:"friendly_name"` + URL string `json:"url"` + Type int `json:"type"` + SubType interface{} `json:"sub_type"` + // keyword_type and sub_type are returned as either an integer or an empty string, // which Go doesn't allow: https://github.com/golang/go/issues/22182 KeywordType interface{} `json:"keyword_type"` KeywordValue string `json:"keyword_value"` diff --git a/pkg/uptimerobot_test.go b/pkg/uptimerobot_test.go index 8b6b829..ce7c4a7 100644 --- a/pkg/uptimerobot_test.go +++ b/pkg/uptimerobot_test.go @@ -132,7 +132,7 @@ func fakeGetMonitorsHandler(req *http.Request) (*http.Response, error) { } func TestGetMonitors(t *testing.T) { - want := []string{"Google", "My Web Page"} + want := []string{"Google", "My Web Page", "My FTP Server"} c := New("dummy") mockClient := MockHTTPClient{ DoFunc: fakeGetMonitorsHandler,