From 3c0bb42db62aabf3c7c79f92921e42339de9eedc Mon Sep 17 00:00:00 2001 From: lil-sahil Date: Fri, 21 Jun 2024 14:02:28 -0400 Subject: [PATCH] Feature: Added endpoint_metadata information to the response struct for 'all' subscription endpoint. Created customString type to handle JSON values that can be either strings or integers. The api response sometimes returns a string or an integer depending on the endpoint that is being hit, so setting the type to a simple int does not resolve the issue - as it will break for other endpoints. - Introduced customString type to manage JSON values that may be strings or integers. - Implemented custom UnmarshalJSON and MarshalJSON methods for customString. - Updated EndpointResponse struct to use customString for fields requiring flexible JSON handling. --- endpoint.go | 33 ++++++++++++++++++++++++++++++--- subscription.go | 3 ++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/endpoint.go b/endpoint.go index bebfaa1..351e37a 100644 --- a/endpoint.go +++ b/endpoint.go @@ -2,6 +2,7 @@ package convoy_go import ( "context" + "encoding/json" "errors" "fmt" "time" @@ -34,11 +35,37 @@ type CreateEndpointRequest struct { RateLimitDuration string `json:"rate_limit_duration,omitempty"` } +// customString is a reusable type to handle JSON values that can be either strings or integers. +// It unmarshals JSON data into a string representation, regardless of whether the input is a string or an integer. +type customString string + +func (c *customString) UnmarshalJSON(b []byte) error { + var strValue string + var intValue int + + if err := json.Unmarshal(b, &strValue); err == nil { + *c = customString(strValue) + return nil + } + + if err := json.Unmarshal(b, &intValue); err == nil { + *c = customString(fmt.Sprintf("%d", intValue)) + return nil + } + + return fmt.Errorf("customstring: cannot unmarshal %v into Go value", string(b)) +} + +func (c *customString) MarshalJSON() ([]byte, error) { + return json.Marshal(string(*c)) +} + type EndpointResponse struct { UID string `json:"uid"` GroupID string `json:"group_id"` OwnerID string `json:"owner_id"` TargetUrl string `json:"target_url"` + URL string `json:"url"` Title string `json:"title"` Description string `json:"description"` @@ -49,9 +76,9 @@ type EndpointResponse struct { SupportEmail string `json:"support_email"` IsDisabled bool `json:"is_disabled"` - HttpTimeout string `json:"http_timeout"` - RateLimit int `json:"rate_limit"` - RateLimitDuration string `json:"rate_limit_duration"` + HttpTimeout customString `json:"http_timeout"` + RateLimit customString `json:"rate_limit"` + RateLimitDuration customString `json:"rate_limit_duration"` Authentication *EndpointAuth `json:"authentication"` Events int64 `json:"events"` diff --git a/subscription.go b/subscription.go index fdf9646..1259931 100644 --- a/subscription.go +++ b/subscription.go @@ -53,7 +53,8 @@ type SubscriptionResponse struct { Type string `json:"type"` Status string `json:"status"` - Source *SourceResponse `json:"source_metadata,omitempty"` + Source *SourceResponse `json:"source_metadata,omitempty"` + EndpointMetaData *EndpointResponse `json:"endpoint_metadata"` // subscription config AlertConfig *AlertConfiguration `json:"alert_config,omitempty"`