2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-11-19 21:11:43 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-11-19 21:11:43 +08:00
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
import "github.com/gogf/gf/v2/container/gvar"
|
2018-11-19 21:11:43 +08:00
|
|
|
|
2019-11-29 22:02:19 +08:00
|
|
|
// SetParam sets custom parameter with key-value pair.
|
2018-11-19 21:11:43 +08:00
|
|
|
func (r *Request) SetParam(key string, value interface{}) {
|
2019-12-01 14:07:36 +08:00
|
|
|
if r.paramsMap == nil {
|
|
|
|
r.paramsMap = make(map[string]interface{})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-12-01 14:07:36 +08:00
|
|
|
r.paramsMap[key] = value
|
2018-11-19 21:11:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:21:05 +08:00
|
|
|
// SetParamMap sets custom parameter with key-value pair map.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// GetParam returns custom parameter with given name `key`.
|
|
|
|
// It returns `def` if `key` does not exist.
|
|
|
|
// It returns nil if `def` is not passed.
|
2021-09-27 21:27:24 +08:00
|
|
|
func (r *Request) GetParam(key string, def ...interface{}) *gvar.Var {
|
2022-03-01 22:53:19 +08:00
|
|
|
if len(r.paramsMap) > 0 {
|
|
|
|
if value, ok := r.paramsMap[key]; ok {
|
|
|
|
return gvar.New(value)
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
if len(def) > 0 {
|
2021-09-27 21:27:24 +08:00
|
|
|
return gvar.New(def[0])
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-09-18 23:20:45 +08:00
|
|
|
return nil
|
|
|
|
}
|