Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions server/session/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package session

import (
"errors"
"net/http"

"github.com/gorilla/sessions"
)

// ErrSessionNotFound is returned when the session is not found in the request context.
var ErrSessionNotFound = errors.New("session not found in request context")

// Get retrieves a value from the session by key.
// Returns the value and true if found, or nil and false if not found.
// Returns an error if the session is not in the request context.
func Get(r *http.Request, key string) (any, bool, error) {
session, ok := r.Context().Value(ctxKey).(*sessions.Session)
if !ok || session == nil {
return nil, false, ErrSessionNotFound
}

val, exists := session.Values[key]
return val, exists, nil
}

// Set stores a value in the session.
// Returns an error if the session is not in the request context.
Comment on lines +26 to +27

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify that sessions are automatically saved when the response is written via the saver middleware, and that this function only modifies the session in memory. Users might expect that calling Set immediately persists the session, but it doesn't - it only modifies the session object which is later saved automatically.

Copilot uses AI. Check for mistakes.
func Set(r *http.Request, w http.ResponseWriter, key string, value any) error {

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The w http.ResponseWriter parameter is accepted but never used in this function. While it may be included for API consistency with the Save function, this could confuse users who might expect the session to be immediately saved when calling Set. Consider either removing this parameter or documenting clearly in the function comment that the session will be automatically saved when the response is written, and this parameter is reserved for future use or API consistency.

Copilot uses AI. Check for mistakes.
session, ok := r.Context().Value(ctxKey).(*sessions.Session)
if !ok || session == nil {
return ErrSessionNotFound
}

session.Values[key] = value
return nil
}

// Delete removes a value from the session by key.
// Returns an error if the session is not in the request context.
Comment on lines +38 to +39

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify that sessions are automatically saved when the response is written via the saver middleware, and that this function only modifies the session in memory. Add a note similar to the one in the Save function documentation to explain when/how the session is persisted.

Copilot uses AI. Check for mistakes.
func Delete(r *http.Request, w http.ResponseWriter, key string) error {

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The w http.ResponseWriter parameter is accepted but never used in this function. While it may be included for API consistency, this could confuse users. Consider either removing this parameter or documenting clearly in the function comment that the session will be automatically saved when the response is written, and this parameter is reserved for future use or API consistency.

Copilot uses AI. Check for mistakes.
session, ok := r.Context().Value(ctxKey).(*sessions.Session)
if !ok || session == nil {
return ErrSessionNotFound
}

delete(session.Values, key)
return nil
}

// Clear removes all values from the session.
// Returns an error if the session is not in the request context.
Comment on lines +50 to +51

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation should clarify that sessions are automatically saved when the response is written via the saver middleware, and that this function only modifies the session in memory. Add a note similar to the one in the Save function documentation to explain when/how the session is persisted.

Copilot uses AI. Check for mistakes.
func Clear(r *http.Request, w http.ResponseWriter) error {

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The w http.ResponseWriter parameter is accepted but never used in this function. While it may be included for API consistency, this could confuse users. Consider either removing this parameter or documenting clearly in the function comment that the session will be automatically saved when the response is written, and this parameter is reserved for future use or API consistency.

Copilot uses AI. Check for mistakes.
session, ok := r.Context().Value(ctxKey).(*sessions.Session)
if !ok || session == nil {
return ErrSessionNotFound
}

for k := range session.Values {
delete(session.Values, k)
}
Comment on lines +58 to +60

Copilot AI Jan 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterating over a map while deleting from it is safe in Go, but it's more idiomatic and clearer to replace the entire Values map with a new empty map. Consider using session.Values = make(map[interface{}]interface{}) or clear(session.Values) (if using Go 1.21+) instead. This makes the intent more explicit and avoids potential confusion.

Suggested change
for k := range session.Values {
delete(session.Values, k)
}
session.Values = make(map[interface{}]interface{})

Copilot uses AI. Check for mistakes.

return nil
}

// Save persists the current session state.
// Note: Sessions are automatically saved when the response is written,
// so this is only needed in special cases like WebSocket upgrades or
// long-running handlers where you want to persist changes early.
func Save(r *http.Request, w http.ResponseWriter) error {
session, ok := r.Context().Value(ctxKey).(*sessions.Session)
if !ok || session == nil {
return ErrSessionNotFound
}

return session.Save(r, w)
}
Loading
Loading