2024-06-29 21:32:57 +03:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
type SessionKey int
|
|
|
|
|
|
|
|
// ID of a session.
|
|
|
|
type ID uint32
|
|
|
|
|
2024-11-09 13:16:11 +02:00
|
|
|
const (
|
2024-06-29 21:32:57 +03:00
|
|
|
idSessionKey SessionKey = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContextWithID returns a new context with the given ID.
|
|
|
|
func ContextWithID(ctx context.Context, id ID) context.Context {
|
|
|
|
return context.WithValue(ctx, idSessionKey, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDFromContext returns ID in this context, or 0 if not contained.
|
|
|
|
func IDFromContext(ctx context.Context) ID {
|
|
|
|
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|