/
ghttp_request_param_param.go
42 lines (37 loc) · 1.14 KB
/
ghttp_request_param_param.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
// Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
package ghttp
import "github.com/gogf/gf/v2/container/gvar"
// SetParam sets custom parameter with key-value pairs.
func (r *Request) SetParam(key string, value interface{}) {
if r.paramsMap == nil {
r.paramsMap = make(map[string]interface{})
}
r.paramsMap[key] = value
}
// SetParamMap sets custom parameter with key-value pair maps.
func (r *Request) SetParamMap(data map[string]interface{}) {
if r.paramsMap == nil {
r.paramsMap = make(map[string]interface{})
}
for k, v := range data {
r.paramsMap[k] = v
}
}
// GetParam returns custom parameter with a given name `key`.
// It returns `def` if `key` does not exist.
// It returns nil if `def` is not passed.
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
if len(r.paramsMap) > 0 {
if value, ok := r.paramsMap[key]; ok {
return gvar.New(value)
}
}
if len(def) > 0 {
return gvar.New(def[0])
}
return nil
}