Simplify context keys

This commit is contained in:
Frédéric Guillot 2018-08-25 09:50:43 -07:00
parent 4148d8af80
commit c327833314
2 changed files with 19 additions and 49 deletions

View file

@ -92,7 +92,7 @@ func (c *Context) PocketRequestToken() string {
return c.getContextStringValue(middleware.PocketRequestTokenContextKey) return c.getContextStringValue(middleware.PocketRequestTokenContextKey)
} }
func (c *Context) getContextStringValue(key *middleware.ContextKey) string { func (c *Context) getContextStringValue(key middleware.ContextKey) string {
if v := c.request.Context().Value(key); v != nil { if v := c.request.Context().Value(key); v != nil {
return v.(string) return v.(string)
} }
@ -100,7 +100,7 @@ func (c *Context) getContextStringValue(key *middleware.ContextKey) string {
return "" return ""
} }
func (c *Context) getContextBoolValue(key *middleware.ContextKey) bool { func (c *Context) getContextBoolValue(key middleware.ContextKey) bool {
if v := c.request.Context().Value(key); v != nil { if v := c.request.Context().Value(key); v != nil {
return v.(bool) return v.(bool)
} }
@ -108,7 +108,7 @@ func (c *Context) getContextBoolValue(key *middleware.ContextKey) bool {
return false return false
} }
func (c *Context) getContextIntValue(key *middleware.ContextKey) int64 { func (c *Context) getContextIntValue(key middleware.ContextKey) int64 {
if v := c.request.Context().Value(key); v != nil { if v := c.request.Context().Value(key); v != nil {
return v.(int64) return v.(int64)
} }

View file

@ -5,51 +5,21 @@
package middleware // import "miniflux.app/middleware" package middleware // import "miniflux.app/middleware"
// ContextKey represents a context key. // ContextKey represents a context key.
type ContextKey struct { type ContextKey int
name string
}
func (c ContextKey) String() string { // List of context keys.
return c.name const (
} UserIDContextKey ContextKey = iota
UserTimezoneContextKey
var ( IsAdminUserContextKey
// UserIDContextKey is the context key used to store the user ID. IsAuthenticatedContextKey
UserIDContextKey = &ContextKey{"UserID"} UserSessionTokenContextKey
UserLanguageContextKey
// UserTimezoneContextKey is the context key used to store the user timezone. UserThemeContextKey
UserTimezoneContextKey = &ContextKey{"UserTimezone"} SessionIDContextKey
CSRFContextKey
// IsAdminUserContextKey is the context key used to store the user role. OAuth2StateContextKey
IsAdminUserContextKey = &ContextKey{"IsAdminUser"} FlashMessageContextKey
FlashErrorMessageContextKey
// IsAuthenticatedContextKey is the context key used to store the authentication flag. PocketRequestTokenContextKey
IsAuthenticatedContextKey = &ContextKey{"IsAuthenticated"}
// UserSessionTokenContextKey is the context key used to store the user session ID.
UserSessionTokenContextKey = &ContextKey{"UserSessionToken"}
// UserLanguageContextKey is the context key to store user language.
UserLanguageContextKey = &ContextKey{"UserLanguageContextKey"}
// UserThemeContextKey is the context key to store user theme.
UserThemeContextKey = &ContextKey{"UserThemeContextKey"}
// SessionIDContextKey is the context key used to store the session ID.
SessionIDContextKey = &ContextKey{"SessionID"}
// CSRFContextKey is the context key used to store CSRF token.
CSRFContextKey = &ContextKey{"CSRF"}
// OAuth2StateContextKey is the context key used to store OAuth2 state.
OAuth2StateContextKey = &ContextKey{"OAuth2State"}
// FlashMessageContextKey is the context key used to store a flash message.
FlashMessageContextKey = &ContextKey{"FlashMessage"}
// FlashErrorMessageContextKey is the context key used to store a flash error message.
FlashErrorMessageContextKey = &ContextKey{"FlashErrorMessage"}
// PocketRequestTokenContextKey is the context key for Pocket Request Token.
PocketRequestTokenContextKey = &ContextKey{"PocketRequestToken"}
) )