/
ghttp_request_param_query.go
148 lines (139 loc) · 4.48 KB
/
ghttp_request_param_query.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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"
"github.com/gogf/gf/v2/util/gconv"
)
// SetQuery sets custom query value with key-value pairs.
func (r *Request) SetQuery(key string, value interface{}) {
r.parseQuery()
if r.queryMap == nil {
r.queryMap = make(map[string]interface{})
}
r.queryMap[key] = value
}
// GetQuery retrieves and return parameter with the given name `key` from query string
// and request body. It returns `def` if `key` does not exist in the query and `def` is given,
// or else it returns nil.
//
// Note that if there are multiple parameters with the same name, the parameters are retrieved
// and overwrote in order of priority: query > body.
func (r *Request) GetQuery(key string, def ...interface{}) *gvar.Var {
r.parseQuery()
if len(r.queryMap) > 0 {
if value, ok := r.queryMap[key]; ok {
return gvar.New(value)
}
}
if r.Method == "GET" {
r.parseBody()
}
if len(r.bodyMap) > 0 {
if v, ok := r.bodyMap[key]; ok {
return gvar.New(v)
}
}
if len(def) > 0 {
return gvar.New(def[0])
}
return nil
}
// GetQueryMap retrieves and returns all parameters passed from the client using HTTP GET method
// as the map. The parameter `kvMap` specifies the keys retrieving from client parameters,
// the associated values are the default values if the client does not pass.
//
// Note that if there are multiple parameters with the same name, the parameters are retrieved and overwrote
// in order of priority: query > body.
func (r *Request) GetQueryMap(kvMap ...map[string]interface{}) map[string]interface{} {
r.parseQuery()
if r.Method == "GET" {
r.parseBody()
}
var m map[string]interface{}
if len(kvMap) > 0 && kvMap[0] != nil {
if len(r.queryMap) == 0 && len(r.bodyMap) == 0 {
return kvMap[0]
}
m = make(map[string]interface{}, len(kvMap[0]))
if len(r.bodyMap) > 0 {
for k, v := range kvMap[0] {
if postValue, ok := r.bodyMap[k]; ok {
m[k] = postValue
} else {
m[k] = v
}
}
}
if len(r.queryMap) > 0 {
for k, v := range kvMap[0] {
if postValue, ok := r.queryMap[k]; ok {
m[k] = postValue
} else {
m[k] = v
}
}
}
} else {
m = make(map[string]interface{}, len(r.queryMap)+len(r.bodyMap))
for k, v := range r.bodyMap {
m[k] = v
}
for k, v := range r.queryMap {
m[k] = v
}
}
return m
}
// GetQueryMapStrStr retrieves and returns all parameters passed from the client using the HTTP GET method as a
// map[string]string. The parameter `kvMap` specifies the keys
// retrieving from client parameters, the associated values are the default values if the client
// does not pass.
func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]string {
queryMap := r.GetQueryMap(kvMap...)
if len(queryMap) > 0 {
m := make(map[string]string, len(queryMap))
for k, v := range queryMap {
m[k] = gconv.String(v)
}
return m
}
return nil
}
// GetQueryMapStrVar retrieves and returns all parameters passed from the client using the HTTP GET method
// as map[string]*gvar.Var. The parameter `kvMap` specifies the keys
// retrieving from client parameters, the associated values are the default values if the client
// does not pass.
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
queryMap := r.GetQueryMap(kvMap...)
if len(queryMap) > 0 {
m := make(map[string]*gvar.Var, len(queryMap))
for k, v := range queryMap {
m[k] = gvar.New(v)
}
return m
}
return nil
}
// GetQueryStruct retrieves all parameters passed from the client using the HTTP GET method
// and converts them to a given struct object. Note that the parameter `pointer` is a pointer
// to the struct object. The optional parameter `mapping` is used to specify the key to
// attribute mapping.
func (r *Request) GetQueryStruct(pointer interface{}, mapping ...map[string]string) error {
_, err := r.doGetQueryStruct(pointer, mapping...)
return err
}
func (r *Request) doGetQueryStruct(pointer interface{}, mapping ...map[string]string) (data map[string]interface{}, err error) {
r.parseQuery()
data = r.GetQueryMap()
if data == nil {
data = map[string]interface{}{}
}
if err = r.mergeDefaultStructValue(data, pointer); err != nil {
return data, nil
}
return data, gconv.Struct(data, pointer, mapping...)
}