From 5c873dd9e63674ebb9d896af6042615c4df743f1 Mon Sep 17 00:00:00 2001 From: Jeremy Cocks Date: Wed, 4 Sep 2024 19:36:46 +0100 Subject: [PATCH 1/3] handle blank AgentLastRuleUpdate --- api.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 553d900..fec2bef 100644 --- a/api.go +++ b/api.go @@ -1857,7 +1857,7 @@ type Agent struct { AgentDecisionTime95th float64 `json:"agent.decision_time_95th"` AgentDecisionTime99th float64 `json:"agent.decision_time_99th"` AgentEnabled bool `json:"agent.enabled"` - AgentLastRuleUpdate time.Time `json:"agent.last_rule_update"` + AgentLastRuleUpdate nullTime `json:"agent.last_rule_update"` AgentLastSeen time.Time `json:"agent.last_seen"` AgentLatencyTime50th float64 `json:"agent.latency_time_50th"` AgentLatencyTime95th float64 `json:"agent.latency_time_95th"` @@ -1900,6 +1900,36 @@ type Agent struct { RuntimeNumGoroutines int `json:"num_goroutines"` } +// customTime wraps time.Time and handles empty string values during unmarshaling. +type nullTime struct { + time.Time +} + +// UnmarshalJSON implements custom unmarshaling for customTime. +// Handles cases where the value is either a valid time string or an empty string. +func (ct *nullTime) UnmarshalJSON(data []byte) error { + // Unmarshaling as a string + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + + // If empty, leave the time as a zero value. + if str == "" { + *ct = nullTime{time.Time{}} + return nil + } + + // Attempt to parse the time string in the expected format. + parsedTime, err := time.Parse(time.RFC3339, str) + if err != nil { + return err + } + + *ct = nullTime{parsedTime} + return nil +} + // agentsResponse is the response for the list agents endpoint type agentsResponse struct { Data []Agent From b0da52199af3e51e0449f78dcee7f17b64f574d7 Mon Sep 17 00:00:00 2001 From: Jeremy Cocks Date: Wed, 4 Sep 2024 19:38:47 +0100 Subject: [PATCH 2/3] rename from customTime to nullTime --- api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.go b/api.go index fec2bef..08bb6f3 100644 --- a/api.go +++ b/api.go @@ -1905,7 +1905,7 @@ type nullTime struct { time.Time } -// UnmarshalJSON implements custom unmarshaling for customTime. +// UnmarshalJSON implements custom unmarshaling for nullTime. // Handles cases where the value is either a valid time string or an empty string. func (ct *nullTime) UnmarshalJSON(data []byte) error { // Unmarshaling as a string From ba633ea5a4ff230c72b209f00be4eebdcd4ff588 Mon Sep 17 00:00:00 2001 From: Jeremy Cocks Date: Wed, 4 Sep 2024 19:41:04 +0100 Subject: [PATCH 3/3] rename type from nullTime to emptyTime --- api.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api.go b/api.go index 08bb6f3..f4a9f9b 100644 --- a/api.go +++ b/api.go @@ -1857,7 +1857,7 @@ type Agent struct { AgentDecisionTime95th float64 `json:"agent.decision_time_95th"` AgentDecisionTime99th float64 `json:"agent.decision_time_99th"` AgentEnabled bool `json:"agent.enabled"` - AgentLastRuleUpdate nullTime `json:"agent.last_rule_update"` + AgentLastRuleUpdate emptyTime `json:"agent.last_rule_update"` AgentLastSeen time.Time `json:"agent.last_seen"` AgentLatencyTime50th float64 `json:"agent.latency_time_50th"` AgentLatencyTime95th float64 `json:"agent.latency_time_95th"` @@ -1900,14 +1900,14 @@ type Agent struct { RuntimeNumGoroutines int `json:"num_goroutines"` } -// customTime wraps time.Time and handles empty string values during unmarshaling. -type nullTime struct { +// emptyTime wraps time.Time and handles empty string values during unmarshaling. +type emptyTime struct { time.Time } -// UnmarshalJSON implements custom unmarshaling for nullTime. +// UnmarshalJSON implements custom unmarshaling for emptyTime. // Handles cases where the value is either a valid time string or an empty string. -func (ct *nullTime) UnmarshalJSON(data []byte) error { +func (ct *emptyTime) UnmarshalJSON(data []byte) error { // Unmarshaling as a string var str string if err := json.Unmarshal(data, &str); err != nil { @@ -1916,7 +1916,7 @@ func (ct *nullTime) UnmarshalJSON(data []byte) error { // If empty, leave the time as a zero value. if str == "" { - *ct = nullTime{time.Time{}} + *ct = emptyTime{time.Time{}} return nil } @@ -1926,7 +1926,7 @@ func (ct *nullTime) UnmarshalJSON(data []byte) error { return err } - *ct = nullTime{parsedTime} + *ct = emptyTime{parsedTime} return nil }