123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package context
- import (
- "context"
- )
- // 定义全局上下文中的键
- type (
- transCtx struct{}
- transLockCtx struct{}
- userIDCtx struct{}
- traceIDCtx struct{}
- fileSizeLimitKey struct{}
- fileNameKey struct{}
- fileExpireKey struct{}
- fileHashKey struct{}
- // FileSizeLimitHandle file size limit
- FileSizeLimitHandle func() bool
- // FileNameHandle the file name
- FileNameHandle func(fileName string) string
- )
- // NewTrans 创建事务的上下文
- func NewTrans(ctx context.Context, trans interface{}) context.Context {
- return context.WithValue(ctx, transCtx{}, trans)
- }
- func NewFileExpireContext(ctx context.Context, v interface{}) context.Context {
- return context.WithValue(ctx, fileExpireKey{}, v)
- }
- func NewFileHashContext(ctx context.Context, v interface{}) context.Context {
- return context.WithValue(ctx, fileHashKey{}, v)
- }
- func FromFileHashContext(ctx context.Context) (interface{}, bool) {
- v := ctx.Value(fileHashKey{})
- return v, v != nil
- }
- func FromFileExpireContext(ctx context.Context) (interface{}, bool) {
- v := ctx.Value(fileExpireKey{})
- return v, v != nil
- }
- // FromTrans 从上下文中获取事务
- func FromTrans(ctx context.Context) (interface{}, bool) {
- v := ctx.Value(transCtx{})
- return v, v != nil
- }
- // NewTransLock 创建事务锁的上下文
- func NewTransLock(ctx context.Context) context.Context {
- return context.WithValue(ctx, transLockCtx{}, struct{}{})
- }
- // FromTransLock 从上下文中获取事务锁
- func FromTransLock(ctx context.Context) bool {
- v := ctx.Value(transLockCtx{})
- return v != nil
- }
- // NewUserID 创建用户ID的上下文
- func NewUserID(ctx context.Context, userID string) context.Context {
- return context.WithValue(ctx, userIDCtx{}, userID)
- }
- // FromUserID 从上下文中获取用户ID
- func FromUserID(ctx context.Context) (string, bool) {
- v := ctx.Value(userIDCtx{})
- if v != nil {
- if s, ok := v.(string); ok {
- return s, s != ""
- }
- }
- return "", false
- }
- // NewTraceID 创建追踪ID的上下文
- func NewTraceID(ctx context.Context, traceID string) context.Context {
- return context.WithValue(ctx, traceIDCtx{}, traceID)
- }
- // FromTraceID 从上下文中获取追踪ID
- func FromTraceID(ctx context.Context) (string, bool) {
- v := ctx.Value(traceIDCtx{})
- if v != nil {
- if s, ok := v.(string); ok {
- return s, s != ""
- }
- }
- return "", false
- }
- // NewFileSizeLimitContext returns a new Context that carries value fsl.
- func NewFileSizeLimitContext(ctx context.Context, fsl FileSizeLimitHandle) context.Context {
- return context.WithValue(ctx, fileSizeLimitKey{}, fsl)
- }
- // FromFileSizeLimitContext returns the FileSizeLimitHandle value stored in ctx, if any.
- func FromFileSizeLimitContext(ctx context.Context) (FileSizeLimitHandle, bool) {
- handle, ok := ctx.Value(fileSizeLimitKey{}).(FileSizeLimitHandle)
- return handle, ok
- }
- // NewFileNameContext returns a new Context that carries value fn.
- func NewFileNameContext(ctx context.Context, fn FileNameHandle) context.Context {
- return context.WithValue(ctx, fileNameKey{}, fn)
- }
- // FromFileNameContext returns the FileNameHandle value stored in ctx, if any.
- func FromFileNameContext(ctx context.Context) (FileNameHandle, bool) {
- handle, ok := ctx.Value(fileNameKey{}).(FileNameHandle)
- return handle, ok
- }
|