From e4b08c7cdfb29a57fa66fcbd1cfceacf82ba84df Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Mon, 9 Oct 2023 11:31:04 +0200 Subject: [PATCH] fix: parsing errors in runtime events --- .../{deployments_test.go => events_test.go} | 0 internal/resources/responses.go | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+) rename internal/cmd/events/{deployments_test.go => events_test.go} (100%) diff --git a/internal/cmd/events/deployments_test.go b/internal/cmd/events/events_test.go similarity index 100% rename from internal/cmd/events/deployments_test.go rename to internal/cmd/events/events_test.go diff --git a/internal/resources/responses.go b/internal/resources/responses.go index 1ae573d5..cc60feef 100644 --- a/internal/resources/responses.go +++ b/internal/resources/responses.go @@ -16,6 +16,7 @@ package resources import ( + "encoding/json" "time" "golang.org/x/oauth2" @@ -187,3 +188,45 @@ type RuntimeEvent struct { FirstSeen time.Time `json:"firstSeen"` LastSeen time.Time `json:"lastSeen"` } + +func (re *RuntimeEvent) UnmarshalJSON(data []byte) error { + type shadowEvent struct { + Type string `json:"type"` + SubObjectPath string `json:"subObjectPath"` + Message string `json:"message"` + Reason string `json:"reason"` + FirstSeen string `json:"firstSeen"` + LastSeen string `json:"lastSeen"` + } + + parseTime := func(timeString string) (time.Time, error) { + if len(timeString) == 0 { + return time.Time{}, nil + } + return time.Parse(time.RFC3339, timeString) + } + + var se shadowEvent + if err := json.Unmarshal(data, &se); err != nil { + return err + } + + re.Type = se.Type + re.Object = se.SubObjectPath + re.Message = se.Message + re.Reason = se.Reason + + firstSeen, err := parseTime(se.FirstSeen) + if err != nil { + return err + } + + lastSeen, err := parseTime(se.LastSeen) + if err != nil { + return err + } + + re.FirstSeen = firstSeen + re.LastSeen = lastSeen + return nil +}