2017-01-26 14:46:44 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
userKey key = iota
|
|
|
|
)
|
|
|
|
|
2017-08-22 09:15:09 -04:00
|
|
|
// ContextWithUser returns a context combined with an User.
|
2017-01-26 14:46:44 -05:00
|
|
|
func ContextWithUser(ctx context.Context, user *User) context.Context {
|
|
|
|
return context.WithValue(ctx, userKey, user)
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:15:09 -04:00
|
|
|
// UserFromContext extracts an User from the given context, if any.
|
2017-01-26 14:46:44 -05:00
|
|
|
func UserFromContext(ctx context.Context) *User {
|
|
|
|
v := ctx.Value(userKey)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(*User)
|
|
|
|
}
|