Skip to content

Commit

Permalink
Improved robustness of context pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
Raajheer1 committed May 24, 2024
1 parent 10030df commit 3d96e72
Showing 1 changed file with 85 additions and 17 deletions.
102 changes: 85 additions & 17 deletions pkg/utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,101 +8,169 @@ import (
type AleKey struct{}

func GetActionLogCtx(r *http.Request) *models.ActionLogEntry {
return r.Context().Value(AleKey{}).(*models.ActionLogEntry)
ale, ok := r.Context().Value(AleKey{}).(*models.ActionLogEntry)
if !ok {
return nil
}
return ale
}

type DleKey struct{}

func GetDisciplinaryLogCtx(r *http.Request) *models.DisciplinaryLogEntry {
return r.Context().Value(DleKey{}).(*models.DisciplinaryLogEntry)
dle, ok := r.Context().Value(DleKey{}).(*models.DisciplinaryLogEntry)
if !ok {
return nil
}
return dle
}

type DocumentKey struct{}

func GetDocumentCtx(r *http.Request) *models.Document {
return r.Context().Value(DocumentKey{}).(*models.Document)
doc, ok := r.Context().Value(DocumentKey{}).(*models.Document)
if !ok {
return nil
}
return doc
}

type FacilityKey struct{}

func GetFacilityCtx(r *http.Request) *models.Facility {
return r.Context().Value(FacilityKey{}).(*models.Facility)
fac, ok := r.Context().Value(FacilityKey{}).(*models.Facility)
if !ok {
return nil
}
return fac
}

type FacilityLogKey struct{}

func GetFacilityLogCtx(r *http.Request) *models.FacilityLogEntry {
return r.Context().Value(FacilityLogKey{}).(*models.FacilityLogEntry)
fle, ok := r.Context().Value(FacilityLogKey{}).(*models.FacilityLogEntry)
if !ok {
return nil
}
return fle
}

type FAQKey struct{}

func GetFAQCtx(r *http.Request) *models.FAQ {
return r.Context().Value(FAQKey{}).(*models.FAQ)
faq, ok := r.Context().Value(FAQKey{}).(*models.FAQ)
if !ok {
return nil
}
return faq
}

type FeedbackKey struct{}

func GetFeedbackCtx(r *http.Request) *models.Feedback {
return r.Context().Value(FeedbackKey{}).(*models.Feedback)
fb, ok := r.Context().Value(FeedbackKey{}).(*models.Feedback)
if !ok {
return nil
}
return fb
}

type NewsKey struct{}

func GetNewsCtx(r *http.Request) *models.News {
return r.Context().Value(NewsKey{}).(*models.News)
news, ok := r.Context().Value(NewsKey{}).(*models.News)
if !ok {
return nil
}
return news
}

type NotificationKey struct{}

func GetNotificationCtx(r *http.Request) *models.Notification {
return r.Context().Value(NotificationKey{}).(*models.Notification)
notif, ok := r.Context().Value(NotificationKey{}).(*models.Notification)
if !ok {
return nil
}
return notif
}

type UserKey struct{}

func GetUserCtx(r *http.Request) *models.User {
return r.Context().Value(UserKey{}).(*models.User)
user, ok := r.Context().Value(UserKey{}).(*models.User)
if !ok {
return nil
}
return user
}

type RosterKey struct{}

func GetRosterCtx(r *http.Request) *models.Roster {
return r.Context().Value(RosterKey{}).(*models.Roster)
roster, ok := r.Context().Value(RosterKey{}).(*models.Roster)
if !ok {
return nil
}
return roster
}

type RatingChangeKey struct{}

func GetRatingChangeCtx(r *http.Request) *models.RatingChange {
return r.Context().Value(RatingChangeKey{}).(*models.RatingChange)
rc, ok := r.Context().Value(RatingChangeKey{}).(*models.RatingChange)
if !ok {
return nil
}
return rc
}

type RosterRequestKey struct{}

func GetRosterRequestCtx(r *http.Request) *models.RosterRequest {
return r.Context().Value(RosterRequestKey{}).(*models.RosterRequest)
rr, ok := r.Context().Value(RosterRequestKey{}).(*models.RosterRequest)
if !ok {
return nil
}
return rr
}

type UserFlagKey struct{}

func GetUserFlagCtx(r *http.Request) *models.UserFlag {
return r.Context().Value(UserFlagKey{}).(*models.UserFlag)
uf, ok := r.Context().Value(UserFlagKey{}).(*models.UserFlag)
if !ok {
return nil
}
return uf
}

type UserRoleKey struct{}

func GetUserRoleCtx(r *http.Request) *models.UserRole {
return r.Context().Value(UserRoleKey{}).(*models.UserRole)
ur, ok := r.Context().Value(UserRoleKey{}).(*models.UserRole)
if !ok {
return nil
}
return ur
}

type XUser struct{}

func GetXUser(r *http.Request) *models.User {
return r.Context().Value(XUser{}).(*models.User)
user, ok := r.Context().Value(XUser{}).(*models.User)
if !ok {
return nil
}
return user
}

type XGuest struct{}

func GetXGuest(r *http.Request) bool {
return r.Context().Value(XGuest{}).(bool)
guest, ok := r.Context().Value(XGuest{}).(bool)
if !ok {
return false
}
return guest
}

0 comments on commit 3d96e72

Please sign in to comment.