github.heygears.com/openimsdk/tools@v0.0.49/mcontext/ctx.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mcontext 16 17 import ( 18 "context" 19 20 "github.com/openimsdk/protocol/constant" 21 "github.com/openimsdk/tools/errs" 22 ) 23 24 var mapper = []string{constant.OperationID, constant.OpUserID, constant.OpUserPlatform, constant.ConnID} 25 26 func WithOpUserIDContext(ctx context.Context, opUserID string) context.Context { 27 return context.WithValue(ctx, constant.OpUserID, opUserID) 28 } 29 30 func WithOpUserPlatformContext(ctx context.Context, platform string) context.Context { 31 return context.WithValue(ctx, constant.OpUserPlatform, platform) 32 } 33 34 func WithTriggerIDContext(ctx context.Context, triggerID string) context.Context { 35 return context.WithValue(ctx, constant.TriggerID, triggerID) 36 } 37 38 func NewCtx(operationID string) context.Context { 39 c := context.Background() 40 ctx := context.WithValue(c, constant.OperationID, operationID) 41 return SetOperationID(ctx, operationID) 42 } 43 44 func SetOperationID(ctx context.Context, operationID string) context.Context { 45 return context.WithValue(ctx, constant.OperationID, operationID) 46 } 47 48 func SetOpUserID(ctx context.Context, opUserID string) context.Context { 49 return context.WithValue(ctx, constant.OpUserID, opUserID) 50 } 51 52 func SetConnID(ctx context.Context, connID string) context.Context { 53 return context.WithValue(ctx, constant.ConnID, connID) 54 } 55 56 func GetOperationID(ctx context.Context) string { 57 if ctx.Value(constant.OperationID) != nil { 58 s, ok := ctx.Value(constant.OperationID).(string) 59 if ok { 60 return s 61 } 62 } 63 return "" 64 } 65 66 func GetOpUserID(ctx context.Context) string { 67 if ctx.Value(constant.OpUserID) != "" { 68 s, ok := ctx.Value(constant.OpUserID).(string) 69 if ok { 70 return s 71 } 72 } 73 return "" 74 } 75 76 func GetConnID(ctx context.Context) string { 77 if ctx.Value(constant.ConnID) != "" { 78 s, ok := ctx.Value(constant.ConnID).(string) 79 if ok { 80 return s 81 } 82 } 83 return "" 84 } 85 86 func GetTriggerID(ctx context.Context) string { 87 if ctx.Value(constant.TriggerID) != "" { 88 s, ok := ctx.Value(constant.TriggerID).(string) 89 if ok { 90 return s 91 } 92 } 93 return "" 94 } 95 96 func GetOpUserPlatform(ctx context.Context) string { 97 if ctx.Value(constant.OpUserPlatform) != "" { 98 s, ok := ctx.Value(constant.OpUserPlatform).(string) 99 if ok { 100 return s 101 } 102 } 103 return "" 104 } 105 106 func GetRemoteAddr(ctx context.Context) string { 107 if ctx.Value(constant.RemoteAddr) != "" { 108 s, ok := ctx.Value(constant.RemoteAddr).(string) 109 if ok { 110 return s 111 } 112 } 113 return "" 114 } 115 116 func GetMustCtxInfo(ctx context.Context) (operationID, opUserID, platform, connID string, err error) { 117 operationID, ok := ctx.Value(constant.OperationID).(string) 118 if !ok { 119 err = errs.ErrArgs.WrapMsg("ctx missing operationID") 120 return 121 } 122 opUserID, ok1 := ctx.Value(constant.OpUserID).(string) 123 if !ok1 { 124 err = errs.ErrArgs.WrapMsg("ctx missing opUserID") 125 return 126 } 127 platform, ok2 := ctx.Value(constant.OpUserPlatform).(string) 128 if !ok2 { 129 err = errs.ErrArgs.WrapMsg("ctx missing platform") 130 return 131 } 132 connID, _ = ctx.Value(constant.ConnID).(string) 133 return 134 } 135 136 func GetCtxInfos(ctx context.Context) (operationID, opUserID, platform, connID string, err error) { 137 operationID, ok := ctx.Value(constant.OperationID).(string) 138 if !ok { 139 err = errs.ErrArgs.WrapMsg("ctx missing operationID") 140 return 141 } 142 opUserID, _ = ctx.Value(constant.OpUserID).(string) 143 platform, _ = ctx.Value(constant.OpUserPlatform).(string) 144 connID, _ = ctx.Value(constant.ConnID).(string) 145 return 146 } 147 148 func WithMustInfoCtx(values []string) context.Context { 149 ctx := context.Background() 150 for i, v := range values { 151 ctx = context.WithValue(ctx, mapper[i], v) 152 } 153 return ctx 154 }