mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-16 09:26:21 -05:00
26 lines
459 B
Go
Executable File
26 lines
459 B
Go
Executable File
package protocol
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type key int
|
|
|
|
const (
|
|
userKey key = iota
|
|
)
|
|
|
|
// ContextWithUser returns a context combined with a User.
|
|
func ContextWithUser(ctx context.Context, user *User) context.Context {
|
|
return context.WithValue(ctx, userKey, user)
|
|
}
|
|
|
|
// UserFromContext extracts a User from the given context, if any.
|
|
func UserFromContext(ctx context.Context) *User {
|
|
v := ctx.Value(userKey)
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.(*User)
|
|
}
|