Skip to content

Commit

Permalink
fix: parsing errors in runtime events
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiola committed Oct 9, 2023
1 parent d17bd9c commit e4b08c7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
File renamed without changes.
43 changes: 43 additions & 0 deletions internal/resources/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package resources

import (
"encoding/json"
"time"

"golang.org/x/oauth2"
Expand Down Expand Up @@ -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
}

0 comments on commit e4b08c7

Please sign in to comment.