diff --git a/api.go b/api.go index 553d900..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 time.Time `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,6 +1900,36 @@ type Agent struct { RuntimeNumGoroutines int `json:"num_goroutines"` } +// emptyTime wraps time.Time and handles empty string values during unmarshaling. +type emptyTime struct { + time.Time +} + +// UnmarshalJSON implements custom unmarshaling for emptyTime. +// Handles cases where the value is either a valid time string or an empty string. +func (ct *emptyTime) 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 = emptyTime{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 = emptyTime{parsedTime} + return nil +} + // agentsResponse is the response for the list agents endpoint type agentsResponse struct { Data []Agent