github.com/kaydxh/golang@v0.0.131/go/context/context.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package context 23 24 import ( 25 "context" 26 "fmt" 27 "strconv" 28 "time" 29 ) 30 31 // RequestIdKey is metadata key name for request ID 32 const ( 33 DefaultHTTPRequestIDKey = "X-Request-ID" 34 ) 35 36 func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { 37 38 if timeout > 0 { 39 return context.WithTimeout(ctx, timeout) 40 } 41 return ctx, func() {} 42 } 43 44 func ExtractStringFromContext(ctx context.Context, key string) string { 45 if v, ok := ctx.Value(key).(string); ok { 46 return v 47 } 48 49 return "" 50 } 51 52 func ExtractIntegerFromContext(ctx context.Context, key string) (int64, error) { 53 v, ok := ctx.Value(key).(string) 54 if !ok { 55 return 0, fmt.Errorf("key[%v] value type is not string", key) 56 } 57 58 number, err := strconv.ParseInt(v, 10, 64) 59 if err != nil { 60 return 0, fmt.Errorf("key[%v] value type is not number: %v", key, err) 61 } 62 63 return number, nil 64 } 65 66 func ExtractFromContext(ctx context.Context, key string) string { 67 switch value := ctx.Value(key).(type) { 68 case string: 69 if value != "" { 70 return value 71 } 72 case []string: 73 if len(value) > 0 { 74 return value[0] 75 } 76 default: 77 return "" 78 } 79 80 return "" 81 } 82 83 func UpdateContext(ctx context.Context, key string, values map[string]interface{}) error { 84 currentValues, ok := ctx.Value(key).(map[string]interface{}) 85 if ok { 86 for k, v := range values { 87 currentValues[k] = v 88 } 89 return nil 90 } 91 92 return fmt.Errorf("key[%v] is not exist in context", key) 93 } 94 95 func SetPairContext(ctx context.Context, key, value string) context.Context { 96 return context.WithValue(ctx, key, value) 97 } 98 99 func AppendContext(ctx context.Context, key string, values ...string) context.Context { 100 currentValues, _ := ctx.Value(key).([]string) 101 currentValues = append(currentValues, values...) 102 return context.WithValue(ctx, key, currentValues) 103 } 104 105 func WithContextRequestId(ctx context.Context, id string) context.Context { 106 return context.WithValue(ctx, DefaultHTTPRequestIDKey, id) 107 } 108 109 func ExtractRequestIDFromContext(ctx context.Context) string { 110 111 if v, ok := ctx.Value(DefaultHTTPRequestIDKey).(string); ok { 112 return v 113 } 114 115 return "" 116 }