/
ghttp_request_param_ctx.go
43 lines (37 loc) · 1.25 KB
/
ghttp_request_param_ctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2017 gf Author(https://github.com/yyf330/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/yyf330/gf.
package ghttp
import (
"context"
"github.com/yyf330/gf/container/gvar"
)
// Context is alias for function GetCtx.
// This function overwrites the http.Request.Context function.
// See GetCtx.
func (r *Request) Context() context.Context {
if r.context == nil {
r.context = r.Request.Context()
}
return r.context
}
// GetCtx retrieves and returns the request's context.
func (r *Request) GetCtx() context.Context {
return r.Context()
}
// GetCtxVar retrieves and returns a Var with given key name.
// The optional parameter <def> specifies the default value of the Var if given <key>
// does not exist in the context.
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var {
value := r.Context().Value(key)
if value == nil && len(def) > 0 {
value = def[0]
}
return gvar.New(value)
}
// SetCtxVar sets custom parameter to context with key-value pair.
func (r *Request) SetCtxVar(key interface{}, value interface{}) {
r.context = context.WithValue(r.Context(), key, value)
}